-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarginalia-md.js
More file actions
210 lines (183 loc) · 6.94 KB
/
marginalia-md.js
File metadata and controls
210 lines (183 loc) · 6.94 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* ═══════════════════════════════════════════════════════════════════════════
Marginalia — Markdown Converter
Transforms a markdown subset into Marginalia-classed HTML.
No dependencies. Pairs with marginalia.css.
═══════════════════════════════════════════════════════════════════════════ */
(function () {
'use strict';
var ALERT_MAP = {
NOTE: 'note', TIP: 'tip', WARNING: 'warning', IMPORTANT: 'important'
};
function escapeHtml(s) {
return s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
}
/* ── Inline transforms ──────────────────────────────────────────────── */
function inlines(line) {
// Badges with type: {text: type}
line = line.replace(/\{([^:}]+):\s*([^}]+)\}/g, function (_, text, type) {
return '<span class="mg-badge" data-type="' + type.trim() + '">' + text.trim() + '</span>';
});
// Badges plain: {text}
line = line.replace(/\{([^}]+)\}/g, '<span class="mg-badge">$1</span>');
// Highlights: ==text==
line = line.replace(/==(.*?)==/g, '<mark class="mg-mark">$1</mark>');
// Inline footnotes: [^N](text)
line = line.replace(/\[\^(\w+)\]\(([^)]+)\)/g,
'<span class="mg-fn"><a class="mg-fn-ref" tabindex="0"></a><span class="mg-fn-body">$2</span></span>');
// Images: 
line = line.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, '<img src="$2" alt="$1">');
// Links: [text](url)
line = line.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');
// Bold: **text**
line = line.replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>');
// Italic: *text*
line = line.replace(/\*(.+?)\*/g, '<em>$1</em>');
// Inline code: `code`
line = line.replace(/`([^`]+)`/g, '<code class="mg-inline-code">$1</code>');
return line;
}
/* ── Block-level converter ──────────────────────────────────────────── */
function convert(md) {
var lines = md.split('\n');
var out = [];
var i = 0;
var para = [];
var dropcap = false;
function flushPara() {
if (!para.length) return;
var cls = dropcap ? ' class="mg-dropcap"' : '';
out.push('<p' + cls + '>' + para.join(' ') + '</p>');
para = [];
dropcap = false;
}
while (i < lines.length) {
var line = lines[i];
// Fenced code block
var fenceMatch = line.match(/^```(\w*)$/);
if (fenceMatch) {
flushPara();
var lang = fenceMatch[1];
var code = [];
i++;
while (i < lines.length && !lines[i].match(/^```\s*$/)) {
code.push(escapeHtml(lines[i]));
i++;
}
var langAttr = lang ? ' data-lang="' + lang + '"' : '';
out.push('<pre class="mg-code"' + langAttr + '><code>' + code.join('\n') + '</code></pre>');
i++; // skip closing fence
continue;
}
// Headings
var headMatch = line.match(/^(#{1,6})\s+(.+)$/);
if (headMatch) {
flushPara();
var level = headMatch[1].length;
out.push('<h' + level + '>' + inlines(headMatch[2]) + '</h' + level + '>');
i++;
continue;
}
// Horizontal rule
if (/^(---|\*\*\*|___)\s*$/.test(line)) {
flushPara();
out.push('<hr>');
i++;
continue;
}
// Dropcap marker
if (/^\{dropcap\}\s*$/.test(line)) {
flushPara();
dropcap = true;
i++;
continue;
}
// Details / collapse passthrough
if (/^<details/.test(line)) {
flushPara();
var detailLines = [];
while (i < lines.length) {
var dl = lines[i];
if (!detailLines.length) {
// Inject mg-collapse if not present
if (dl.indexOf('mg-collapse') === -1) {
dl = dl.replace('<details', '<details class="mg-collapse"');
}
}
detailLines.push(dl);
if (/<\/details>/.test(lines[i])) { i++; break; }
i++;
}
out.push(detailLines.join('\n'));
continue;
}
// Blockquote / alert block
if (/^>\s/.test(line)) {
flushPara();
var bqLines = [];
while (i < lines.length && /^>\s?/.test(lines[i])) {
bqLines.push(lines[i].replace(/^>\s?/, ''));
i++;
}
emitBlockquote(bqLines, out);
continue;
}
// Blank line — flush paragraph
if (/^\s*$/.test(line)) {
flushPara();
i++;
continue;
}
// Paragraph text
para.push(inlines(line));
i++;
}
flushPara();
return out.join('\n');
}
/* ── Blockquote dispatch ────────────────────────────────────────────── */
function emitBlockquote(bqLines, out) {
var first = bqLines[0] || '';
var alertMatch = first.match(/^\[!(\w+)\]\s*$/);
var content;
if (alertMatch) {
var keyword = alertMatch[1].toUpperCase();
content = bqLines.slice(1).map(inlines);
// Callout variants
if (ALERT_MAP[keyword]) {
var type = ALERT_MAP[keyword];
var label = keyword.charAt(0) + keyword.slice(1).toLowerCase();
var body = content.length
? '<p><strong>' + label + ':</strong> ' + content.join(' ') + '</p>'
: '';
out.push('<div class="mg-callout" data-type="' + type + '">' + body + '</div>');
return;
}
// Sidebar
if (keyword === 'ASIDE') {
out.push('<aside class="mg-sidebar"><p>' + content.join(' ') + '</p></aside>');
return;
}
// Margin note
if (keyword === 'MARGIN') {
out.push('<div class="mg-margin"><p>' + content.join(' ') + '</p></div>');
return;
}
// Explicit pull quote
if (keyword === 'QUOTE') {
out.push('<aside class="mg-pull"><blockquote>' + content.join(' ') + '</blockquote></aside>');
return;
}
}
// Plain blockquote → pull quote
content = bqLines.map(inlines);
out.push('<aside class="mg-pull"><blockquote>' + content.join(' ') + '</blockquote></aside>');
}
/* ── Export ──────────────────────────────────────────────────────────── */
if (typeof window !== 'undefined') {
window.Marginalia = window.Marginalia || {};
window.Marginalia.md = convert;
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = { convert: convert };
}
})();