Skip to content

Commit 694b091

Browse files
committed
add reveal.js-fragment support
1 parent c247429 commit 694b091

File tree

4 files changed

+138
-10
lines changed

4 files changed

+138
-10
lines changed

docs-obsidian.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,11 @@ Those work on normal image-links, like the standard DM ones and the shortcut-lin
7272
# And as short-link
7373
![[my-img.png|120]]
7474
```
75+
## Fragments in Reveal.js
76+
You can use fragments.
77+
Fragment lines have `#fragment` in the line above them.
78+
For blocks you may also have a `#fragment-start` followed by a `#fragment-end`.
79+
It is important to note that those flags must be on their own line.
80+
81+
This then will become a fragment in the presentation-view.
82+
It will be invisible in the normal view.

md/test-exam-practice-question.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ class question
3030
```bash
3131
class question
3232
```
33-
* What development tools will you be using?
34-
* Make a plan.
35-
* Explain the steps to get to a finished new product.
33+
- What development tools will you be using?
34+
- Make a plan.
35+
- Explain the steps to get to a finished new product.
3636
@@@ #answer
3737
## Answer
3838
```plantuml
@@ -41,7 +41,7 @@ class answer
4141
```bash
4242
class answer
4343
```
44-
* Java and Javascript.
45-
* Just do it.
46-
* Do it. See, you're done.
44+
- Java and Javascript.
45+
- Just do it.
46+
- Do it. See, you're done.
4747
@@@

md/test-md-file.md

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ You may add images...
9393
### Global Images (URL)
9494
Add them using an absolute image-url like so:
9595
`https://raw.githubusercontent.com/UnterrainerInformatik/java-http-server/master/docs/standard-request-response-process.png`
96-
9796
![global-image|100x300](https://raw.githubusercontent.com/UnterrainerInformatik/java-http-server/master/docs/standard-request-response-process.png)
9897

9998
### Obsidian Link Images
@@ -109,4 +108,70 @@ Example:
109108
### PDF
110109
![[Test.pdf]]
111110
### Docx
112-
![[Test.docx]]
111+
![[Test.docx]]
112+
## Lists And Fragments
113+
### Lists
114+
- one
115+
- two
116+
- three
117+
118+
+ one
119+
+ two
120+
+ three
121+
122+
* one
123+
* two
124+
* three
125+
126+
1. one
127+
2. two
128+
3. three
129+
### Fragments
130+
#fragment
131+
Fragmented Text.
132+
#fragment
133+
- one
134+
#fragment
135+
- two
136+
#fragment
137+
![global-image|100x300](https://raw.githubusercontent.com/UnterrainerInformatik/java-http-server/master/docs/standard-request-response-process.png)
138+
139+
#fragment
140+
another text
141+
142+
#fragment-start
143+
- Fragment 1
144+
- Fragment 2
145+
- Fragment 3
146+
#fragment-end
147+
148+
#fragment
149+
- one
150+
#fragment
151+
- two
152+
#fragment
153+
- three
154+
155+
#fragment
156+
+ one
157+
#fragment
158+
+ two
159+
#fragment
160+
+ three
161+
162+
#fragment
163+
* one
164+
#fragment
165+
* two
166+
#fragment
167+
* three
168+
169+
#fragment
170+
1. one
171+
#fragment
172+
2. two
173+
#fragment
174+
3. three
175+
176+
#fragment
177+
Done.

obsidian.js

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ const internalSubstitutions = {
2525
string: "@#@##@@--@code@--@@##@#@",
2626
regexp: /@#@##@@--@code@--@@##@#@/gms,
2727
},
28+
fragment_single: {
29+
string: "<!-- __fragment-single__ -->"
30+
},
31+
fragment_start: {
32+
string: "<!-- __fragment-start__ -->"
33+
},
34+
fragment_end: {
35+
string: "<!-- __fragment-end__ -->"
36+
}
2837
};
2938
let codeList = [];
3039
let openNavTreeScript = "";
@@ -369,6 +378,7 @@ export async function preParse(md, req) {
369378
r = preMarkCode(r);
370379
r = preReplaceObsidianFileLinks(r, req);
371380
r = preMarkCallouts(r);
381+
r = preprocessFragments(r)
372382
r = unmarkCode(r);
373383
return r;
374384
}
@@ -447,6 +457,50 @@ function getFollowingQuotedLines(md, index) {
447457
return { trimmedLines: lines.slice(0, i).join("\n"), originalLength };
448458
}
449459

460+
function preprocessFragments(md) {
461+
return md
462+
.replace(/^[ \t]*#fragment-start[ \t]*$/gm, internalSubstitutions.fragment_start.string)
463+
.replace(/^[ \t]*#fragment-end[ \t]*$/gm, internalSubstitutions.fragment_end.string)
464+
465+
.replace(/^[ \t]*#fragment[ \t]*\n([^\n]+)/gm, (_, nextLine) => {
466+
if (/^\s*[-*+1.]\s/.test(nextLine)) {
467+
return nextLine.replace(/^(\s*[-*+1.]\s*)(.+)/, (_, prefix, content) => {
468+
return `${prefix}%%LI_FRAGMENT%% ${content}`;
469+
});
470+
}
471+
return `<span class="fragment">${nextLine.trim()}</span>`;
472+
});
473+
}
474+
475+
function postprocessFragments(html) {
476+
const { fragment_start, fragment_end } = internalSubstitutions;
477+
478+
// Block fragments
479+
html = html.replace(
480+
new RegExp(`${fragment_start.string}([\\s\\S]*?)${fragment_end.string}([\\s\\S]*?</li>)`, 'g'),
481+
(fullMatch, content, trailingLi) => {
482+
let fixed = (content + trailingLi).trim();
483+
484+
// Remove <p> inside <li>
485+
fixed = fixed.replace(
486+
/<li>\s*\n?\s*<p>([\s\S]*?)<\/p>\s*\n?\s*<\/li>/gm,
487+
(_, inner) => `<li>${inner.trim()}</li>`
488+
);
489+
490+
return `<div class="fragment">\n${fixed}\n</div>\n\n`;
491+
}
492+
);
493+
494+
495+
// Inline fragment: list item
496+
html = html.replace(
497+
/<li[^>]*>\s*(?:<p>)?%%LI_FRAGMENT%%\s*(.*?)(?:<\/p>)?\s*<\/li>/g,
498+
(_, content) => `<li class="fragment">${content.trim()}</li>`
499+
);
500+
501+
return html;
502+
}
503+
450504
function preReplaceObsidianFileLinks(html, req) {
451505
const regex = /(?<!\!)\[\[([^\]\n]+)\]\]/g;
452506

@@ -472,8 +526,8 @@ function preReplaceObsidianFileLinks(html, req) {
472526
}
473527
const lastPartOfFileName = fileName.split("/").pop();
474528
const filePath = mdFilesMap[lastPartOfFileName];
475-
console.log(fileName)
476-
console.log(mdFilesMap)
529+
// console.log(fileName)
530+
// console.log(mdFilesMap)
477531
if (filePath) {
478532
let f = filePath[0];
479533
if (filePath.length > 1) {
@@ -543,6 +597,7 @@ export function manipulateHtml(html, req) {
543597
r = replacePreMarkCallouts(r);
544598
r = replaceObsidianImageLinks(r, req);
545599
r = replaceObsidianImageAltResizeValues(r);
600+
r = postprocessFragments(r)
546601
r = makeContentMap(r);
547602
return r;
548603
}

0 commit comments

Comments
 (0)