Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
circles-android
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Circles
circles-android
Commits
42c64735
Commit
42c64735
authored
1 year ago
by
Taras
Browse files
Options
Downloads
Patches
Plain Diff
Update markdown parser
parent
87904979
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
app/src/main/java/org/futo/circles/feature/timeline/post/markdown/MarkdownParser.kt
+2
-80
2 additions, 80 deletions
.../circles/feature/timeline/post/markdown/MarkdownParser.kt
with
2 additions
and
80 deletions
app/src/main/java/org/futo/circles/feature/timeline/post/markdown/MarkdownParser.kt
+
2
−
80
View file @
42c64735
package
org.futo.circles.feature.timeline.post.markdown
import
android.content.Context
import
android.graphics.Color
import
android.graphics.Typeface
import
android.text.Editable
import
android.text.style.StrikethroughSpan
import
android.text.style.StyleSpan
import
androidx.core.content.ContextCompat
import
androidx.core.text.getSpans
import
io.noties.markwon.AbstractMarkwonPlugin
import
io.noties.markwon.Markwon
import
io.noties.markwon.MarkwonSpansFactory
import
io.noties.markwon.core.spans.BulletListItemSpan
import
io.noties.markwon.core.spans.EmphasisSpan
import
io.noties.markwon.core.spans.LinkSpan
import
io.noties.markwon.core.spans.StrongEmphasisSpan
import
io.noties.markwon.ext.strikethrough.StrikethroughPlugin
import
io.noties.markwon.ext.tasklist.TaskListPlugin
import
io.noties.markwon.ext.tasklist.TaskListSpan
import
io.noties.markwon.linkify.LinkifyPlugin
import
org.commonmark.node.Emphasis
import
org.commonmark.node.StrongEmphasis
import
org.futo.circles.R
import
org.futo.circles.core.extensions.notEmptyDisplayName
import
org.futo.circles.core.provider.MatrixSessionProvider
import
org.futo.circles.extensions.getGivenSpansAt
import
org.futo.circles.feature.timeline.post.markdown.mentions.plugin.MentionPlugin
import
org.futo.circles.feature.timeline.post.markdown.span.MentionSpan
import
org.futo.circles.feature.timeline.post.markdown.span.OrderedListItemSpan
import
org.futo.circles.feature.timeline.post.markdown.span.TextStyle
import
org.matrix.android.sdk.api.session.getUserOrDefault
object
MarkdownParser
{
const
val
mentionMark
=
"@"
private
const
val
boldMark
=
"**"
private
const
val
italicMark
=
"_"
private
const
val
strikeMark
=
"~~"
private
const
val
notDoneMark
=
"* [ ]"
private
const
val
doneMark
=
"* [x]"
fun
editableToMarkdown
(
text
:
Editable
):
String
{
val
textCopy
=
Editable
.
Factory
.
getInstance
().
newEditable
(
text
)
text
.
getGivenSpansAt
(
span
=
TextStyle
.
values
()).
forEach
{
val
start
=
textCopy
.
getSpanStart
(
it
)
val
end
=
textCopy
.
getSpanEnd
(
it
)
when
(
it
)
{
is
StrongEmphasisSpan
->
{
val
endIndex
=
calculateLastIndexToInsert
(
textCopy
,
end
,
boldMark
)
textCopy
.
insert
(
start
,
boldMark
)
textCopy
.
insert
(
endIndex
,
boldMark
)
}
is
EmphasisSpan
->
{
val
endIndex
=
calculateLastIndexToInsert
(
textCopy
,
end
,
italicMark
)
textCopy
.
insert
(
start
,
italicMark
)
textCopy
.
insert
(
endIndex
,
italicMark
)
}
is
StrikethroughSpan
->
{
val
endIndex
=
calculateLastIndexToInsert
(
textCopy
,
end
,
strikeMark
)
textCopy
.
insert
(
start
,
strikeMark
)
textCopy
.
insert
(
endIndex
,
strikeMark
)
}
is
LinkSpan
->
{
val
linkStartMark
=
"["
textCopy
.
insert
(
start
,
linkStartMark
)
textCopy
.
insert
(
end
+
linkStartMark
.
length
,
"](${it.link})"
)
}
is
BulletListItemSpan
->
textCopy
.
insert
(
start
,
"*"
)
is
OrderedListItemSpan
->
textCopy
.
insert
(
start
,
it
.
number
)
is
TaskListSpan
->
{
val
taskSpanMark
=
if
(
it
.
isDone
)
doneMark
else
notDoneMark
textCopy
.
insert
(
start
,
taskSpanMark
)
}
}
}
text
.
getSpans
<
MentionSpan
>().
forEach
{
val
end
=
textCopy
.
getSpanEnd
(
it
)
val
textToInsert
=
it
.
name
+
mentionMark
textCopy
.
insert
(
end
,
textToInsert
)
}
return
textCopy
.
toString
()
}
fun
markwonBuilder
(
context
:
Context
):
Markwon
=
Markwon
.
builder
(
context
)
.
usePlugin
(
StrikethroughPlugin
.
create
())
.
usePlugin
(
LinkifyPlugin
.
create
())
.
usePlugin
(
MentionPlugin
(
context
))
.
usePlugin
(
TaskListPlugin
.
create
(
ContextCompat
.
getColor
(
context
,
R
.
color
.
blue
),
ContextCompat
.
getColor
(
context
,
R
.
color
.
blue
),
Color
.
WHITE
)
).
build
()
.
build
()
fun
markwonNotificationBuilder
(
context
:
Context
):
Markwon
=
Markwon
.
builder
(
context
)
.
usePlugin
(
object
:
AbstractMarkwonPlugin
()
{
...
...
@@ -116,13 +44,7 @@ object MarkdownParser {
})
.
usePlugin
(
StrikethroughPlugin
.
create
())
.
usePlugin
(
LinkifyPlugin
.
create
())
.
usePlugin
(
TaskListPlugin
.
create
(
ContextCompat
.
getColor
(
context
,
R
.
color
.
blue
),
ContextCompat
.
getColor
(
context
,
R
.
color
.
blue
),
Color
.
WHITE
)
).
build
()
.
build
()
fun
hasCurrentUserMention
(
text
:
String
):
Boolean
{
val
session
=
MatrixSessionProvider
.
currentSession
?:
return
false
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment