Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions FSNotesCore/Business/Note.swift
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ public class Note: NSObject {
result += "<hr>\n\n"
}

result += list.joined()
result += list.joined(separator: "---")

return result
}
Expand All @@ -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<hr>\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<hr>\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<hr>\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<hr>\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<hr>\n")

return result
}

public func overwrite(url: URL) {
self.url = url

Expand Down
52 changes: 48 additions & 4 deletions Resources/MPreview.bundle/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@

<script charset="utf-8" src="{WEB_PATH}js/highlight.min.js" type="text/javascript"></script>
<script>hljs.initHighlightingOnLoad();</script>
<script async src="{WEB_PATH}js/mermaid.min.js"></script>
<script src="{WEB_PATH}js/mermaid.min.js"></script>
<script src="{WEB_PATH}js/elk-layout.min.js"></script>

<script>
(function () {
Expand Down Expand Up @@ -321,22 +322,65 @@
}

let loadMemaid = () => {
if (typeof mermaid === 'undefined') return;

let mermaidTheme = 'default';
if ('{FSNOTES_APPEARANCE}' == 'darkmode') {
mermaidTheme = 'dark';
}

// Register ELK layout engine if available
if (typeof window.__elkLayouts !== 'undefined') {
try {
mermaid.registerLayoutLoaders(window.__elkLayouts);
} catch (e) {}
}

var config = {
startOnLoad: true,
startOnLoad: false,
theme: mermaidTheme,
flowchart: {
useMaxWidth: false,
useMaxWidth: true,
htmlLabels: true
}
};

mermaid.initialize(config);
window.mermaid.init(undefined, document.querySelectorAll('.language-mermaid'));

var nodes = document.querySelectorAll('.language-mermaid');

// Normalize YAML frontmatter indentation in mermaid blocks
// (tabs and mixed whitespace are invalid YAML)
nodes.forEach(function(el) {
var text = el.textContent;
if (text.trimStart().startsWith('---')) {
var lines = text.split('\n');
var inFrontmatter = false;
for (var i = 0; i < lines.length; i++) {
var trimmed = lines[i].trim();
if (trimmed === '---') {
inFrontmatter = !inFrontmatter;
continue;
}
if (inFrontmatter && trimmed.length > 0) {
if (lines[i] !== trimmed) {
// Any indented line gets normalized to 2-space indent
lines[i] = ' ' + trimmed;
}
}
}
el.textContent = lines.join('\n');
}
});

mermaid.run({ nodes: nodes }).catch(function(e) {
nodes.forEach(function(el) {
var errDiv = document.createElement('div');
errDiv.style.cssText = 'color:#ff4444;padding:8px;font-size:12px;';
errDiv.textContent = 'Mermaid error: ' + e.message;
el.parentNode.insertBefore(errDiv, el.nextSibling);
});
});
};

let loadAttachments = () => {
Expand Down
27 changes: 27 additions & 0 deletions Resources/MPreview.bundle/js/elk-layout.min.js

Large diffs are not rendered by default.