Skip to content

Commit 7c963bf

Browse files
committed
update
1 parent 7dac955 commit 7c963bf

File tree

3 files changed

+40
-5341
lines changed

3 files changed

+40
-5341
lines changed

main.js

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,37 @@ function cleanPreview(el) {
2020
const text = node.textContent?.trim();
2121
if (!text)
2222
return;
23+
// Check for @@@ blocks, ##fragment, and ##side-by-side blocks
2324
if (/^ *@{3} *$/.test(text) ||
24-
/^ *@{3} +([^\s,]+([ ,]+[^\s,]+)*)$/.test(text)) {
25-
node.classList.add("safelearn-hidden");
25+
/^ *@{3} +([^\s,]+([ ,]+[^\s,]+)*)$/.test(text) ||
26+
/^##fragment$/.test(text) ||
27+
/^##(side-by-side-(start|end)|separator)$/.test(text)) {
28+
node.addClass("safelearn-hidden");
2629
return;
2730
}
28-
if (/^##fragment$/.test(text)) {
29-
node.classList.add("safelearn-hidden");
30-
return;
31-
}
32-
if (/^##(side-by-side-(start|end)|separator)$/.test(text)) {
33-
node.classList.add("safelearn-hidden");
34-
return;
35-
}
36-
node.innerHTML = node.innerHTML
37-
.replace(/(##fragment)(?![\w-])/gi, '<span class="safelearn-hidden">$1</span>')
38-
.replace(/@@@(?: +[^\s,]+([ ,]+[^\s,]+)*)?/g, '<span class="safelearn-hidden">$&</span>')
39-
.replace(/##(side-by-side-(start|end)|separator)/g, '<span class="safelearn-hidden">$&</span>');
31+
// Remove @@@ blocks, ##fragment, and ##side-by-side blocks from text nodes
32+
node.childNodes.forEach(child => {
33+
if (child.nodeType === Node.TEXT_NODE) {
34+
let changed = false;
35+
let newText = child.textContent ?? "";
36+
const replacePatterns = [
37+
/@@@(?: +[^\s,]+([ ,]+[^\s,]+)*)?/g,
38+
/##fragment(?![\w-])/gi,
39+
/##(side-by-side-(start|end)|separator)/gi
40+
];
41+
for (const pattern of replacePatterns) {
42+
if (pattern.test(newText)) {
43+
newText = newText.replace(pattern, "");
44+
changed = true;
45+
}
46+
}
47+
if (changed) {
48+
const span = document.createElement("span");
49+
span.textContent = newText;
50+
child.replaceWith(span);
51+
}
52+
}
53+
});
4054
});
4155
}
4256
const safelearnHighlighter = view.ViewPlugin.fromClass(class {
@@ -61,10 +75,17 @@ const safelearnHighlighter = view.ViewPlugin.fromClass(class {
6175
const line = doc.line(i);
6276
const text = line.text.trim();
6377
// === ##fragment ===
64-
const fragIndex = line.text.indexOf("##fragment");
78+
let fragIndex = line.text.indexOf("##fragment ");
79+
if (fragIndex === -1) {
80+
fragIndex = line.text.indexOf("##fragment");
81+
// only if the line ends with this "##fragment" without any trailing text
82+
if (fragIndex !== -1 && line.text.slice(fragIndex + "##fragment".length).trim() !== "") {
83+
fragIndex = -1;
84+
}
85+
}
6586
if (fragIndex !== -1) {
6687
const from = line.from + fragIndex;
67-
const to = from + "##fragment".length;
88+
const to = from + "##fragment ".length;
6889
decorations.push({
6990
from,
7091
to,

manifest.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"id": "safelearn-plugin",
3-
"name": "SafeLearn Plugin",
2+
"id": "safelearn-formatter",
3+
"name": "SafeLearn Formatter",
44
"version": "1.0.1",
55
"minAppVersion": "1.5.7",
6-
"description": "This plugin offers visual aids for SafeLearn-specific tags such as ##fragment, permission blocks, and side-by-side layouts for Reveal.js.",
6+
"description": "Offers visual aids for tags specific for SafeLearn (an open-source project) such as ##fragment, permission blocks, and side-by-side layouts for Reveal.js.",
77
"author": "UnterrainerInformatik",
88
"authorUrl": "https://github.com/UnterrainerInformatik",
99
"repo": "UnterrainerInformatik/safeLearn-Obsidian-plugin",

0 commit comments

Comments
 (0)