Skip to content

Commit 078449d

Browse files
Re-apply hack to the markdown plugin
This commit attempts to show the hack I did to allow h2 to be horizontal slides and h3 vertical, which requires preserving the `#` characters.
1 parent 867bf41 commit 078449d

1 file changed

Lines changed: 74 additions & 33 deletions

File tree

  • presentations/2025-01-27-IGC/plugin/markdown

presentations/2025-01-27-IGC/plugin/markdown/markdown.js

Lines changed: 74 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/**
2+
* Right now this allows:
3+
* <section
4+
* data-markdown="content.md"
5+
* data-separator-vertical="^### "
6+
* data-separator="^## "
7+
* ></section>
8+
*
9+
* I like this because you can very quickly map h1 to title, h2 to horizontal slides, and h3 to vertical.
10+
* Which is how I think of it.
11+
*
12+
* But, what if you want an image? Or you don't want the H1 to show?
13+
*
14+
* It would be nice to have some other way to indicate a section... I wonder if you can match h2 OR
15+
* <!-- SECTION -->
16+
* for a horizontal
17+
* or some other keyword...
18+
*/
119
!(function (e, t) {
220
"object" === typeof exports && "undefined" !== typeof module
321
? (module.exports = t())
@@ -2007,42 +2025,65 @@
20072025
"</script>"
20082026
);
20092027
}
2010-
function i(e, t) {
2011-
t = s(t);
2012-
const n = new RegExp(
2013-
t.separator + (t.verticalSeparator ? "|" + t.verticalSeparator : ""),
2028+
function slidify(markdown, options) {
2029+
options = s(options);
2030+
const separatorRegex = new RegExp(
2031+
options.separator +
2032+
(options.verticalSeparator ? "|" + options.verticalSeparator : ""),
20142033
"mg"
20152034
),
2016-
i = new RegExp(t.separator);
2017-
let l,
2018-
o,
2019-
a,
2020-
c = 0,
2021-
h = !0,
2022-
p = [];
2023-
for (; (l = n.exec(e)); )
2024-
(o = i.test(l[0])),
2025-
!o && h && p.push([]),
2026-
(a = e.substring(c, l.index)),
2027-
o && h ? p.push(a) : p[p.length - 1].push(a),
2028-
(c = n.lastIndex),
2029-
(h = o);
2030-
(h ? p : p[p.length - 1]).push(e.substring(c));
2031-
let u = "";
2032-
for (let e = 0, n = p.length; e < n; e++)
2033-
p[e] instanceof Array
2034-
? ((u += "<section " + t.attributes + ">"),
2035-
p[e].forEach(function (e) {
2036-
u += "<section data-markdown>" + r(e, t) + "</section>";
2035+
horizontalSeparatorRegex = new RegExp(options.separator);
2036+
let matches,
2037+
isHorizontal,
2038+
content,
2039+
lastIndex = 0,
2040+
wasHorizontal = !0,
2041+
sectionStack = [];
2042+
for (; (matches = separatorRegex.exec(markdown)); ) {
2043+
isHorizontal = horizontalSeparatorRegex.test(matches[0]);
2044+
if (!isHorizontal && wasHorizontal) {
2045+
sectionStack.push([]);
2046+
}
2047+
content = markdown.substring(lastIndex, matches.index);
2048+
// START MY HACK ================
2049+
// Do not add empty slides
2050+
if (content === "") {
2051+
// do nothing
2052+
}
2053+
// END MY HACK ===================
2054+
else if (isHorizontal && wasHorizontal) {
2055+
sectionStack.push(content);
2056+
} else {
2057+
sectionStack[sectionStack.length - 1].push(content);
2058+
}
2059+
// START MY HACK ================
2060+
// This was the original line:
2061+
// lastIndex = separatorRegex.lastIndex;
2062+
// This is my line, which preserves the matched symbols (eg ##):
2063+
lastIndex = separatorRegex.lastIndex - matches[0].length;
2064+
// END MY HACK ===================
2065+
wasHorizontal = isHorizontal;
2066+
}
2067+
(wasHorizontal
2068+
? sectionStack
2069+
: sectionStack[sectionStack.length - 1]
2070+
).push(markdown.substring(lastIndex));
2071+
let markdownSections = "";
2072+
for (let e = 0, n = sectionStack.length; e < n; e++)
2073+
sectionStack[e] instanceof Array
2074+
? ((markdownSections += "<section " + options.attributes + ">"),
2075+
sectionStack[e].forEach(function (e) {
2076+
markdownSections +=
2077+
"<section data-markdown>" + r(e, options) + "</section>";
20372078
}),
2038-
(u += "</section>"))
2039-
: (u +=
2079+
(markdownSections += "</section>"))
2080+
: (markdownSections +=
20402081
"<section " +
2041-
t.attributes +
2082+
options.attributes +
20422083
" data-markdown>" +
2043-
r(p[e], t) +
2084+
r(sectionStack[e], options) +
20442085
"</section>");
2045-
return u;
2086+
return markdownSections;
20462087
}
20472088
function l(e) {
20482089
return new Promise(function (s) {
@@ -2086,7 +2127,7 @@
20862127
});
20872128
})(e).then(
20882129
function (t, s) {
2089-
e.outerHTML = i(t.responseText, {
2130+
e.outerHTML = slidify(t.responseText, {
20902131
separator: e.getAttribute("data-separator"),
20912132
verticalSeparator: e.getAttribute(
20922133
"data-separator-vertical"
@@ -2105,7 +2146,7 @@
21052146
}
21062147
)
21072148
)
2108-
: (e.outerHTML = i(t(e), {
2149+
: (e.outerHTML = slidify(t(e), {
21092150
separator: e.getAttribute("data-separator"),
21102151
verticalSeparator: e.getAttribute("data-separator-vertical"),
21112152
notesSeparator: e.getAttribute("data-separator-notes"),
@@ -2219,7 +2260,7 @@
22192260
},
22202261
processSlides: l,
22212262
convertSlides: c,
2222-
slidify: i,
2263+
slidify: slidify,
22232264
marked: P,
22242265
};
22252266
};

0 commit comments

Comments
 (0)