-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarkdown-it-slides.js
158 lines (136 loc) · 4.47 KB
/
markdown-it-slides.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"use strict";
const HSEP = "---";
module.exports = function revealjs_plugin(md) {
function isSep(token) {
return token.type === "hr" && token.markup === HSEP;
}
function renderOpening(tokens, idx, options, env, slf) {
return `<${tokens[idx].tag}${slf.renderAttrs(tokens[idx])}>`;
}
function renderClosing(tokens, idx, options, env, slf) {
return `</${tokens[idx].tag}>`;
}
md.renderer.rules.pres_open = renderOpening;
md.renderer.rules.pres_close = renderClosing;
md.renderer.rules.slide_open = renderOpening;
md.renderer.rules.slide_close = renderClosing;
function nextDivider(tokens, start) {
for (let i = start; i < tokens.length; i++) {
if (isSep(tokens[i])) {
return i;
}
}
return -1;
}
var openSlides = 0;
function presentationOpen(state) {
var token = new state.Token("pres_open", "section", 1);
token.block = true;
token.attrs = [["class", "reveal"]];
return token;
}
function presentationClose(state) {
return new state.Token("pres_close", "section", -1);
}
function slidesOpen(state) {
var token = new state.Token("slides_open", "div", 1);
token.block = true;
token.attrs = [["class", "slides"]];
return token;
}
function slidesClose(state) {
return new state.Token("slides_close", "div", -1);
}
function slideOpen(state, slideCount) {
openSlides++;
var token = new state.Token("slide_open", "section", 1);
token.attrs = [
["class", "slide"],
["id", slideCount],
];
return token;
}
function slideClose(state) {
openSlides--;
return new state.Token("slide_close", "section", -1);
}
md.core.ruler.push("revealjs", function (state) {
let divIdx = 0;
let slideCount = 0;
while (true) {
divIdx = nextDivider(state.tokens, divIdx);
if (divIdx === -1) {
break;
}
let divider = state.tokens[divIdx];
if (isSep(divider) && openSlides === 0) {
state.tokens.unshift(slideOpen(state, slideCount));
slideCount++;
divIdx++; // we added a token at the beginning, we need to update divIdx
}
let tags = [];
if (isSep(divider)) {
while (openSlides > 0) {
tags.push(slideClose(state));
}
tags.push(slideOpen(state, slideCount));
slideCount++;
// because "---" is hijacked from plain markdown, it translates
// to one token which we remove
state.tokens.splice(divIdx, 1, ...tags);
}
}
while (openSlides > 0) {
state.tokens.push(slideClose(state));
}
state.tokens.unshift(slidesOpen(state));
state.tokens.unshift(presentationOpen(state));
state.tokens.push(slidesClose(state));
state.tokens.push(presentationClose(state));
});
};
// function slidify(md) {
// md.core.ruler.push("slidify", function (state) {
// const dividers = state.tokens.filter((t) => t.type === "hr");
// const count = dividers.length;
// dividers.forEach((token, index) => {
// const prev = index - 1 < 0 ? count - 1 : index - 1;
// const next = index + 1;
// const replacement = new state.Token("html_block", "", 0);
// replacement.content = `
// ${nav(prev, next, index, count)}
// </section>
// <section id="${next}" class="slide">
// `;
// const originalIndex = state.tokens.indexOf(token);
// state.tokens.splice(originalIndex, 1, replacement);
// });
// const open = new state.Token("html_block", "", 0);
// open.content = `<section id="0" class="slide">`;
// state.tokens.unshift(open);
// const close = new state.Token("html_block", "", 0);
// close.content = `
// ${nav(count - 2, 0, count, count)}
// </section>
// `;
// state.tokens.push(close);
// });
// }
// function nav(prev, next, index, total) {
// return `
// <nav>
// <a href="#${prev}" class="button" aria-label="Previous">
// <svg width="36" height="36" fill="none" viewBox="0 0 24 24" stroke="currentColor">
// <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7" />
// </svg>
// </a>
// <a href="#${next}" class="button" aria-label="Next">
// <svg width="36" height="36" fill="none" viewBox="0 0 24 24" stroke="currentColor">
// <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" />
// </svg>
// </a>
// <span class="count">${index + 1} / ${total + 1}</span>
// </nav>
// `;
// }
// module.exports = slidify;