-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod.ts
113 lines (103 loc) · 4.17 KB
/
mod.ts
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
/**
* WASM functions for processing Markdown text and MJML, written in Rust. See README.md for
* examples.
*/
import { instantiate } from "./lib/parsedown.generated.js";
interface MarkdownToHtmlOKOutput {
errors?: never;
headings: { heading: string; id: string }[];
html: string;
statistics: {
reading_time: number;
word_count: number;
};
}
interface MarkdownToHtmlErrorOutput {
errors: string[];
headings?: never;
html?: never;
statistics?: never;
}
interface MarkdownToHtmlOptions {
canonicalRootUrl?: string;
enableSmartPunctuation?: string;
searchTerm?: string;
}
type MarkdownToPlaintextOptions = Omit<MarkdownToHtmlOptions, "searchTerm">;
/**
* Convert the, input, `markdown` string to HTML using a [CommonMark](https://commonmark.org/)
* Markdown Parser.
*
* @param markdown The Markdown text to parse
* @param {MarkdownToHtmlOptions|undefined} [options={}] - Parse options
* @param {boolean} options.enableSmartPunctuation - `true` if "something" should be replaced with
* “something”, etc.
* @param {string} options.canonicalRootUrl - if included, relative url gain this value as a prefix
* (`/home` becomes `https://example.com/home`)
* @param {string} options.searchTerm - if included, output HTML wraps any instances of this value
* in `mark` tags (`A senctence with the-search-term` becomes
* `A sentence with <mark>the-search-term</mark>`), for use in
* highlighting search results, with CSS, for example. The
* first instance also has `id=search-match` added the mark
* tag. You might use this to scroll the first match into view
* automatically.
* @returns {Promise<MarkdownToHtmlOKOutput|MarkdownToHtmlErrorOutput>} `markdown` parsed into HTML as an object or an error object. If successful, the HTML is
* in the `.html` field of the returned object.
*/
const markdownToHtml: (
markdown: string,
options: MarkdownToHtmlOptions | undefined,
) => Promise<MarkdownToHtmlOKOutput | MarkdownToHtmlErrorOutput> =
async function markdownToHtml(
markdown,
options: MarkdownToHtmlOptions | undefined,
): Promise<MarkdownToHtmlOKOutput | MarkdownToHtmlErrorOutput> {
const { markdown_to_html } = await instantiate();
return markdown_to_html(markdown, {
enable_smart_punctuation: true,
...(typeof options?.canonicalRootUrl !== "undefined"
? { canonical_root_url: options.canonicalRootUrl }
: {}),
...(typeof options?.enableSmartPunctuation !== "undefined"
? { enable_smart_punctuation: options.enableSmartPunctuation }
: {}),
...(typeof options?.searchTerm !== "undefined"
? { search_term: options.searchTerm }
: {}),
});
};
/**
* Convert the, input, `markdown` string to plaintext, to use, for example in a broadcast email or
* RSS feed.
*
* @param markdown The Markdown text to parse
* @returns `markdown` parsed into a plaintext string
*/
const markdownToPlaintext: (
markdown: string,
options?: MarkdownToPlaintextOptions,
) => Promise<string> = async function markdownToPlaintext(markdown, options) {
const { markdown_to_plaintext } = await instantiate();
const { canonicalRootUrl, enableSmartPunctuation } = options ?? {};
return markdown_to_plaintext(markdown, {
...(typeof canonicalRootUrl !== "undefined"
? { canonical_root_url: canonicalRootUrl }
: {}),
...(typeof enableSmartPunctuation !== "undefined"
? { enable_smart_punctuation: enableSmartPunctuation }
: {}),
});
};
/**
* Convert the, input, `mjml` string to HTML, for use in a broadcast email, for example.
*
* @param markdown The Markdown text to parse
* @returns `markdown` parsed into a plaintext string
*/
const mjmlToHtml: (mjml: string) => Promise<string> = async function mjmlToHtml(
mjml,
) {
const { mjml_to_html } = await instantiate();
return mjml_to_html(mjml);
};
export { markdownToHtml, markdownToPlaintext, mjmlToHtml };