diff --git a/FSNotesCore/Business/Note.swift b/FSNotesCore/Business/Note.swift index 5bf24a7cb..9ab776009 100644 --- a/FSNotesCore/Business/Note.swift +++ b/FSNotesCore/Business/Note.swift @@ -879,7 +879,7 @@ public class Note: NSObject { result += "
\n\n" } - result += list.joined() + result += list.joined(separator: "---") return result } @@ -892,15 +892,42 @@ public class Note: NSObject { #if IOS_APP || os(OSX) let mutable = NotesTextProcessor.convertAppTags(in: self.content.unloadAttachments(), codeBlockRanges: codeBlockRangesCache) let content = NotesTextProcessor.convertAppLinks(in: mutable, codeBlockRanges: codeBlockRangesCache) - let result = cleanMetaData(content: content.string) - .replacingOccurrences(of: "\n---\n", with: "\n
\n") - + let cleaned = cleanMetaData(content: content.string) + let result = Note.replaceHorizontalRulesOutsideCodeBlocks(cleaned) + return result #else return cleanMetaData(content: self.content.string) #endif } + /// Replace `\n---\n` with `\n
\n` only outside fenced code blocks, + /// so that `---` inside e.g. mermaid YAML frontmatter is preserved. + private static func replaceHorizontalRulesOutsideCodeBlocks(_ text: String) -> String { + let pattern = "(?<=\\n|\\A)```[^\\n]*\\n[\\s\\S]*?\\n```(?=\\n|\\Z)" + guard let regex = try? NSRegularExpression(pattern: pattern) else { + return text.replacingOccurrences(of: "\n---\n", with: "\n
\n") + } + + let nsText = text as NSString + let codeRanges = regex.matches(in: text, range: NSRange(location: 0, length: nsText.length)).map { $0.range } + + var result = "" + var currentIndex = 0 + + for codeRange in codeRanges { + let beforeRange = NSRange(location: currentIndex, length: codeRange.location - currentIndex) + result += nsText.substring(with: beforeRange).replacingOccurrences(of: "\n---\n", with: "\n
\n") + result += nsText.substring(with: codeRange) + currentIndex = codeRange.location + codeRange.length + } + + let remainingRange = NSRange(location: currentIndex, length: nsText.length - currentIndex) + result += nsText.substring(with: remainingRange).replacingOccurrences(of: "\n---\n", with: "\n
\n") + + return result + } + public func overwrite(url: URL) { self.url = url diff --git a/Resources/MPreview.bundle/index.html b/Resources/MPreview.bundle/index.html index a14c707af..78a6955fb 100644 --- a/Resources/MPreview.bundle/index.html +++ b/Resources/MPreview.bundle/index.html @@ -31,7 +31,8 @@ - + +