@@ -18,6 +18,7 @@ import com.yangdai.opennote.presentation.theme.linkColor
1818import com.yangdai.opennote.presentation.util.extension.highlight.Highlight
1919import com.yangdai.opennote.presentation.util.extension.highlight.HighlightExtension
2020import com.yangdai.opennote.presentation.util.extension.properties.Properties.splitPropertiesAndContent
21+ import org.commonmark.ext.front.matter.YamlFrontMatterExtension
2122import org.commonmark.ext.gfm.strikethrough.Strikethrough
2223import org.commonmark.ext.gfm.strikethrough.StrikethroughExtension
2324import org.commonmark.ext.gfm.tables.TableRow
@@ -48,7 +49,8 @@ val PARSER: Parser =
4849 StrikethroughExtension .create(),
4950 InsExtension .create(),
5051 HighlightExtension .create(),
51- TablesExtension .create()
52+ TablesExtension .create(),
53+ YamlFrontMatterExtension .create()
5254 )
5355 ).includeSourceSpans(IncludeSourceSpans .BLOCKS_AND_INLINES ).build()
5456
@@ -413,26 +415,24 @@ fun findTagRanges(text: String): StyleRanges {
413415 }
414416
415417 override fun visit (fencedCodeBlock : FencedCodeBlock ) {
416- val span = fencedCodeBlock.sourceSpans.firstOrNull()
417- if (span != null ) {
418- // Get the opening fence marker (```language)
419- val openingFence = span.inputIndex
420- val infoString = fencedCodeBlock.info?.length ? : 0
421- val openingMarkerLength = fencedCodeBlock.openingFenceLength ? : return
422-
423- markerRanges.add(openingFence until (openingFence + openingMarkerLength)) // ```
424- fencedCodeBlockInfoRanges.add(openingFence + openingMarkerLength until (openingFence + openingMarkerLength + infoString)) // language
425-
426- val blockContentLength =
427- if (fencedCodeBlock.literal.isEmpty()) 0 else fencedCodeBlock.literal.length + 1
428- val fence =
429- openingFence + openingMarkerLength + infoString + blockContentLength
430- codeBlockContentRanges.add((openingFence + openingMarkerLength + infoString) until fence) // content
431-
432- val closingMarkerLength = fencedCodeBlock.closingFenceLength ? : return
433- if (fence + closingMarkerLength <= text.length) {
434- markerRanges.add(fence until (fence + closingMarkerLength))
435- }
418+ val span = fencedCodeBlock.sourceSpans.firstOrNull() ? : return
419+
420+ // Get the opening fence marker (```language)
421+ val openingFenceStartIndex = span.inputIndex
422+ val openingMarkerLength = fencedCodeBlock.openingFenceLength ? : return
423+ val infoStringStartIndex = openingFenceStartIndex + openingMarkerLength
424+ markerRanges.add(openingFenceStartIndex until infoStringStartIndex) // ```
425+ val infoStringLength = fencedCodeBlock.info?.length ? : 0
426+ fencedCodeBlockInfoRanges.add(infoStringStartIndex until (infoStringStartIndex + infoStringLength)) // language
427+
428+ val closingMarkerLength = fencedCodeBlock.closingFenceLength ? : return
429+ val blockContentLength =
430+ if (fencedCodeBlock.literal.isEmpty()) 0 else fencedCodeBlock.literal.length + 1
431+ val fence =
432+ openingFenceStartIndex + openingMarkerLength + infoStringLength + blockContentLength
433+ codeBlockContentRanges.add((openingFenceStartIndex + openingMarkerLength + infoStringLength) until fence) // content
434+ if (fence + closingMarkerLength <= text.length) {
435+ markerRanges.add(fence until (fence + closingMarkerLength))
436436 }
437437 }
438438
@@ -470,31 +470,42 @@ fun parseMarkdownContent(text: String): AnnotatedString {
470470 val styleRanges = findTagRanges(textWithoutProperties)
471471
472472 return buildAnnotatedString {
473+ fun safeAddStyle (style : SpanStyle , start : Int , end : Int ) {
474+ val safeStart = start.coerceAtLeast(0 ).coerceAtMost(text.length)
475+ val safeEnd = end.coerceAtLeast(0 ).coerceAtMost(text.length)
476+ addStyle(style, safeStart, safeEnd)
477+ }
478+
479+ fun safeAddStyle (style : ParagraphStyle , start : Int , end : Int ) {
480+ val safeStart = start.coerceAtLeast(0 ).coerceAtMost(text.length)
481+ val safeEnd = end.coerceAtLeast(0 ).coerceAtMost(text.length)
482+ addStyle(style, safeStart, safeEnd)
483+ }
473484 styleRanges.apply {
474485 codeRanges.forEach { range ->
475- addStyle (CODE_STYLE , range.first, range.last + 1 )
476- addStyle (SYMBOL_STYLE , range.first, range.first + 1 )
477- addStyle (SYMBOL_STYLE , range.last - 1 + 1 , range.last + 1 )
486+ safeAddStyle (CODE_STYLE , range.first, range.last + 1 )
487+ safeAddStyle (SYMBOL_STYLE , range.first, range.first + 1 )
488+ safeAddStyle (SYMBOL_STYLE , range.last - 1 + 1 , range.last + 1 )
478489 }
479490 boldItalicRanges.forEach { range ->
480- addStyle (BOLD_ITALIC_STYLE , range.first, range.last + 1 )
481- addStyle (SYMBOL_STYLE , range.first, range.first + 3 )
482- addStyle (SYMBOL_STYLE , range.last - 3 + 1 , range.last + 1 )
491+ safeAddStyle (BOLD_ITALIC_STYLE , range.first, range.last + 1 )
492+ safeAddStyle (SYMBOL_STYLE , range.first, range.first + 3 )
493+ safeAddStyle (SYMBOL_STYLE , range.last - 3 + 1 , range.last + 1 )
483494 }
484495 boldRanges.forEach { range ->
485- addStyle (BOLD_STYLE , range.first, range.last + 1 )
486- addStyle (SYMBOL_STYLE , range.first, range.first + 2 )
487- addStyle (SYMBOL_STYLE , range.last - 2 + 1 , range.last + 1 )
496+ safeAddStyle (BOLD_STYLE , range.first, range.last + 1 )
497+ safeAddStyle (SYMBOL_STYLE , range.first, range.first + 2 )
498+ safeAddStyle (SYMBOL_STYLE , range.last - 2 + 1 , range.last + 1 )
488499 }
489500 italicRanges.forEach { range ->
490- addStyle (ITALIC_STYLE , range.first, range.last + 1 )
491- addStyle (SYMBOL_STYLE , range.first, range.first + 1 )
492- addStyle (SYMBOL_STYLE , range.last - 1 + 1 , range.last + 1 )
501+ safeAddStyle (ITALIC_STYLE , range.first, range.last + 1 )
502+ safeAddStyle (SYMBOL_STYLE , range.first, range.first + 1 )
503+ safeAddStyle (SYMBOL_STYLE , range.last - 1 + 1 , range.last + 1 )
493504 }
494505 highlightRanges.forEach { range ->
495- addStyle (HIGHLIGHT_STYLE , range.first, range.last + 1 )
496- addStyle (SYMBOL_STYLE , range.first, range.first + 2 )
497- addStyle (SYMBOL_STYLE , range.last - 2 + 1 , range.last + 1 )
506+ safeAddStyle (HIGHLIGHT_STYLE , range.first, range.last + 1 )
507+ safeAddStyle (SYMBOL_STYLE , range.first, range.first + 2 )
508+ safeAddStyle (SYMBOL_STYLE , range.last - 2 + 1 , range.last + 1 )
498509 }
499510
500511 val combinedRanges = (strikethroughRanges + underlineRanges).distinct()
@@ -507,36 +518,36 @@ fun parseMarkdownContent(text: String): AnnotatedString {
507518 hasUnderline -> UNDERLINE_STYLE
508519 else -> return @forEach
509520 }
510- addStyle (style, range.first, range.last + 1 )
521+ safeAddStyle (style, range.first, range.last + 1 )
511522 }
512523
513524 strikethroughRanges.forEach { range ->
514- addStyle (SYMBOL_STYLE , range.first, range.first + 2 )
515- addStyle (SYMBOL_STYLE , range.last - 2 + 1 , range.last + 1 )
525+ safeAddStyle (SYMBOL_STYLE , range.first, range.first + 2 )
526+ safeAddStyle (SYMBOL_STYLE , range.last - 2 + 1 , range.last + 1 )
516527 }
517528 underlineRanges.forEach { range ->
518- addStyle (SYMBOL_STYLE , range.first, range.first + 2 )
519- addStyle (SYMBOL_STYLE , range.last - 2 + 1 , range.last + 1 )
529+ safeAddStyle (SYMBOL_STYLE , range.first, range.first + 2 )
530+ safeAddStyle (SYMBOL_STYLE , range.last - 2 + 1 , range.last + 1 )
520531 }
521532
522533 headerRanges.forEach { (range, level) ->
523- addStyle (HEADER_STYLES [level - 1 ], range.first, range.last + 1 )
524- addStyle (HEADER_LINE_STYLES [level - 1 ], range.first, range.last + 1 )
525- addStyle (SYMBOL_STYLE , range.first, range.first + level + 1 )
534+ safeAddStyle (HEADER_STYLES [level - 1 ], range.first, range.last + 1 )
535+ safeAddStyle (HEADER_LINE_STYLES [level - 1 ], range.first, range.last + 1 )
536+ safeAddStyle (SYMBOL_STYLE , range.first, range.first + level + 1 )
526537 }
527538
528539 // Add styling for list markers
529540 markerRanges.forEach { range ->
530- addStyle (MARKER_STYLE , range.first, range.last + 1 )
541+ safeAddStyle (MARKER_STYLE , range.first, range.last + 1 )
531542 }
532543 linkRanges.forEach { range ->
533- addStyle (LINK_STYLE , range.first, range.last + 1 )
544+ safeAddStyle (LINK_STYLE , range.first, range.last + 1 )
534545 }
535546 fencedCodeBlockInfoRanges.forEach { range ->
536- addStyle (KEYWORD_STYLE , range.first, range.last + 1 )
547+ safeAddStyle (KEYWORD_STYLE , range.first, range.last + 1 )
537548 }
538549 codeBlockContentRanges.forEach { range ->
539- addStyle (CODE_BLOCK_STYLE , range.first, range.last + 1 )
550+ safeAddStyle (CODE_BLOCK_STYLE , range.first, range.last + 1 )
540551 }
541552 }
542553 append(textWithoutProperties)
0 commit comments