@rettangoli/sites 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.js ADDED
File without changes
@@ -0,0 +1,101 @@
1
+ /**
2
+ * Code taken from https://github.com/antfu/markdown-it-async
3
+ * Modified to remove <pre>... wrapper
4
+ * Converted from TypeScript to JavaScript using esbuild
5
+ */
6
+
7
+ import MarkdownIt from "markdown-it";
8
+ const placeholder = (id, code) => `<pre><!--::markdown-it-async::${id}::--><code>${code}</code></pre>`;
9
+ const placeholderRe = /<pre><!--::markdown-it-async::(\w+)::--><code>[\s\S]*?<\/code><\/pre>/g;
10
+ function randStr() {
11
+ return Math.random().toString(36).slice(2) + Math.random().toString(36).slice(2);
12
+ }
13
+ export class MarkdownItAsync extends MarkdownIt {
14
+ placeholderMap;
15
+ disableWarn = false;
16
+ constructor(...args) {
17
+ const map = /* @__PURE__ */ new Map();
18
+ const options = args.length === 2 ? args[1] : args[0];
19
+ if (options && "highlight" in options)
20
+ options.highlight = wrapHightlight(options.highlight, map);
21
+ super(...args);
22
+ this.placeholderMap = map;
23
+ }
24
+ // implementation
25
+ use(plugin, ...params) {
26
+ return super.use(plugin, ...params);
27
+ }
28
+ render(src, env) {
29
+ if (this.options.warnOnSyncRender && !this.disableWarn) {
30
+ console.warn("[markdown-it-async] Please use `md.renderAsync` instead of `md.render`");
31
+ }
32
+ return super.render(src, env);
33
+ }
34
+ async renderAsync(src, env) {
35
+ this.options.highlight = wrapHightlight(this.options.highlight, this.placeholderMap);
36
+ this.disableWarn = true;
37
+ const result = this.render(src, env);
38
+ this.disableWarn = false;
39
+ return replaceAsync(result, placeholderRe, async (match, id) => {
40
+ if (!this.placeholderMap.has(id))
41
+ throw new Error(`Unknown highlight placeholder id: ${id}`);
42
+ const [promise, _str, lang, _attrs] = this.placeholderMap.get(id);
43
+ const result2 = await promise || "";
44
+ this.placeholderMap.delete(id);
45
+ if (result2.startsWith("<pre"))
46
+ return result2;
47
+ else
48
+ return result2;
49
+ });
50
+ }
51
+ }
52
+ export function createMarkdownItAsync(...args) {
53
+ return new MarkdownItAsync(...args);
54
+ }
55
+ export function replaceAsync(string, searchValue, replacer) {
56
+ try {
57
+ if (typeof replacer === "function") {
58
+ const values = [];
59
+ String.prototype.replace.call(string, searchValue, (...args) => {
60
+ values.push(replacer(...args));
61
+ return "";
62
+ });
63
+ return Promise.all(values).then((resolvedValues) => {
64
+ return String.prototype.replace.call(string, searchValue, () => {
65
+ return resolvedValues.shift() || "";
66
+ });
67
+ });
68
+ } else {
69
+ return Promise.resolve(
70
+ String.prototype.replace.call(string, searchValue, replacer)
71
+ );
72
+ }
73
+ } catch (error) {
74
+ return Promise.reject(error);
75
+ }
76
+ }
77
+ const wrappedSet = /* @__PURE__ */ new WeakSet();
78
+ function escapeHtml(unsafe) {
79
+ return unsafe.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;");
80
+ }
81
+ function wrapHightlight(highlight, map) {
82
+ if (!highlight)
83
+ return void 0;
84
+ if (wrappedSet.has(highlight))
85
+ return highlight;
86
+ const wrapped = (str, lang, attrs) => {
87
+ const promise = highlight(str, lang, attrs);
88
+ if (typeof promise === "string")
89
+ return promise;
90
+ const id = randStr();
91
+ map.set(id, [promise, str, lang, attrs]);
92
+ let code = str;
93
+ if (code.endsWith("\n"))
94
+ code = code.slice(0, -1);
95
+ code = escapeHtml(code);
96
+ return placeholder(id, code);
97
+ };
98
+ wrappedSet.add(wrapped);
99
+ return wrapped;
100
+ }
101
+ export default createMarkdownItAsync;