-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod.ts
100 lines (90 loc) · 3.1 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
/**
* 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 options.enableSmartPunctuation `true` if "something" should be replaced with
* “something”, etc.
* @returns `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,
) => Promise<MarkdownToHtmlOKOutput | MarkdownToHtmlErrorOutput> =
async function markdownToHtml(markdown, options) {
const { markdown_to_html } = await instantiate();
const { canonicalRootUrl, enableSmartPunctuation, searchTerm } = options ??
{};
return markdown_to_html(markdown, {
enable_smart_punctuation: true,
...(typeof canonicalRootUrl !== "undefined"
? { canonical_root_url: canonicalRootUrl }
: {}),
...(typeof enableSmartPunctuation !== "undefined"
? { enable_smart_punctuation: enableSmartPunctuation }
: {}),
...(typeof searchTerm !== "undefined" ? { search_term: 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 };