generated from shgysk8zer0/npm-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown.js
More file actions
180 lines (148 loc) · 5.12 KB
/
markdown.js
File metadata and controls
180 lines (148 loc) · 5.12 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
import { Marked } from 'marked';
import { markedHighlight } from 'marked-highlight';
import { stringify } from '@aegisjsproject/core/stringify.js';
import hljs from 'highlight.js/core.min.js';
import plaintext from 'highlight.js/languages/plaintext.min.js';
export const hljsURL = new URL(`https://unpkg.com/@highlightjs/cdn-assets@${hljs.versionString}/`);
export const registerLanguage = (name, def) => hljs.registerLanguage(name, def);
export const registerLanguages = langsObj => Object.entries(langsObj)
.forEach(([name, lang]) => registerLanguage(name, lang));
export const listLanguages = () => hljs.listLanguages();
export const getLanguage = lang => hljs.getLanguage(lang);
export const getLanguagesObject = () => Object.fromEntries(listLanguages().map(lang => [lang, getLanguage(lang)]));
registerLanguage('plaintext', plaintext);
const sluggify = str => str.trim().replaceAll(/[^A-Za-z0-9]+/g, '-').toLowerCase();
export function createStyleSheet(path, { media, base = hljsURL } = {}) {
const link = document.createElement('link');
link.relList.add('stylesheet');
link.crossOrigin = 'anonymous';
link.referrerPolicy = 'no-referrer';
if (typeof media === 'string') {
link.media = media;
} else if (media instanceof MediaQueryList) {
link.media = media.media;
}
link.href = new URL(`./styles/${path}.min.css`, base);
return link;
}
export function parse(input, {
gfm = true,
breaks = false,
silent = false,
langPrefix = 'hljs language-',
fallbackLang = 'plaintext',
addHeadingIDs = true,
idPrefix = null,
allowElements,
allowAttributes,
allowCustomElements,
allowUnknownMarkup,
allowComments,
} = {}) {
const marked = new Marked(
markedHighlight({
langPrefix,
highlight(code, lang) {
const language = hljs.getLanguage(lang) ? lang : fallbackLang;
return hljs.highlight(code, { language }).value;
}
})
);
const frag = document.createDocumentFragment();
let raw = input.replaceAll(/\\`/g, '`');
if (String.dedent instanceof Function && raw.startsWith('\n') && /\n\t*$/.test(raw)) {
const tmp = [raw];
tmp.raw = [raw];
Object.freeze(tmp);
raw = String.dedent(tmp);
}
const parsed = marked.parse(raw, { gfm, breaks, silent });
const doc = Document.parseHTML(parsed, {
allowElements, allowAttributes, allowCustomElements, allowUnknownMarkup,
allowComments,
});
frag.append(...doc.body.childNodes);
if (addHeadingIDs) {
frag.querySelectorAll('h1, h2, h3, h4, h5, h6').forEach(heading => {
heading.id = typeof idPrefix === 'string' ? `${idPrefix}-${sluggify(heading.textContent)}` : sluggify(heading.textContent);
});
}
return frag;
}
export function createMDParser({
gfm = true,
breaks = false,
silent = false,
langPrefix = 'hljs language-',
fallbackLang = 'plaintext',
addHeadingIDs = true,
idPrefix = null,
languages,
allowElements,
allowAttributes,
allowCustomElements,
allowUnknownMarkup,
allowComments,
} = {}) {
if (typeof languages === 'object' && languages !== null) {
registerLanguages(languages);
}
const marked = new Marked(
markedHighlight({
langPrefix,
highlight(code, lang) {
const language = hljs.getLanguage(lang) ? lang : fallbackLang;
return hljs.highlight(code, { language }).value;
}
})
);
return (strings, ...args) => {
const frag = document.createDocumentFragment();
let raw = String.raw(strings, ...args.map(stringify)).replaceAll(/\\`/g, '`');
if (String.dedent instanceof Function && raw.startsWith('\n') && /\n\t*$/.test(raw)) {
const tmp = [raw];
tmp.raw = [raw];
Object.freeze(tmp);
raw = String.dedent(tmp);
}
const parsed = marked.parse(raw, { gfm, breaks, silent });
const doc = Document.parseHTML(parsed, {
allowElements, allowAttributes, allowCustomElements, allowUnknownMarkup,
allowComments,
});
frag.append(...doc.body.childNodes);
if (addHeadingIDs) {
frag.querySelectorAll('h1, h2, h3, h4, h5, h6').forEach(heading => {
heading.id = typeof idPrefix === 'string' ? `${idPrefix}-${sluggify(heading.textContent)}` : sluggify(heading.textContent);
});
}
return frag;
};
}
export const md = createMDParser({});
export async function getMarkdown(url, {
mode = 'cors',
referrerPolicy = 'no-referrer',
parser = md,
headers = new Headers({ Accept: 'text/markdown' }),
...rest
} = {}) {
if (typeof headers === 'object' && ! (headers instanceof Headers)) {
return await getMarkdown(url, { mode, referrerPolicy, parser, headers: new Headers(headers), ...rest });
} else if (! headers.has('Accept')) {
headers.set('Accept', 'text/markdown');
}
const resp = await fetch(url, { mode, referrerPolicy, headers, ...rest }).catch(() => Response.error());
if (! resp.ok) {
throw new DOMException(`${resp.url} [${resp.status}]`, 'NetworkError');
} else if (! resp.headers.get('Content-Type').startsWith('text/markdown')) {
throw new TypeError(`Invalid Content-Type: ${resp.headers.get('Content-Type')}.`);
} else {
return parser`${await resp.text()}`;
}
}
export async function loadLanguage(lang) {
await import(`${hljsURL}es/languages/${lang}.min.js`)
.then(mod => registerLanguage(lang, mod.default));
}
export const loadLanguages = async (...langs) => Promise.all(langs.map(loadLanguage));