deno.land / x / lume@v2.1.4 / plugins / remark.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
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
import loader from "../core/loaders/text.ts";import { merge } from "../core/utils/object.ts";import { rehypeRaw, rehypeSanitize, rehypeStringify, remarkGfm, remarkParse, remarkRehype, unified,} from "../deps/remark.ts";
import type Site from "../core/site.ts";import type { Engine, Helper } from "../core/renderer.ts";import type { Page } from "../core/file.ts";import type { PluggableList, RehypeOptions } from "../deps/remark.ts";
export interface Options { /** List of extensions this plugin applies to */ extensions?: string[];
/** * List of remark plugins to use * @default `[remarkGfm]` */ remarkPlugins?: PluggableList;
/** Options to pass to rehype */ rehypeOptions?: RehypeOptions;
/** List of rehype plugins to use */ rehypePlugins?: PluggableList;
/** Flag to turn on HTML sanitization to prevent XSS */ sanitize?: boolean;
/** Set `false` to remove the default plugins */ useDefaultPlugins?: boolean;}
// Default optionsexport const defaults: Options = { extensions: [".md"], sanitize: false, useDefaultPlugins: true, rehypeOptions: { allowDangerousHtml: true, },};
const remarkDefaultPlugins = [ remarkGfm,];
/** Template engine to render Markdown files with Remark */export class MarkdownEngine implements Engine { engine: unified.Processor;
constructor(engine: unified.Processor) { this.engine = engine; }
deleteCache() {}
async render( content: string, data?: Record<string, unknown>, filename?: string, ): Promise<string> { const page = data?.page as Page | undefined; return (await this.engine.process({ value: content, data: page?.data, path: filename, })).toString(); }
renderComponent( content: string, data?: Record<string, unknown>, filename?: string, ): string { const page = data?.page as Page | undefined; return this.engine.processSync({ value: content, data: page?.data, path: filename, }).toString(); }
addHelper() {}}
/** Register the plugin to support Markdown */export default function (userOptions?: Options) { const options = merge(defaults, userOptions);
return function (site: Site) { const plugins = [];
// Add remark-parse to generate MDAST plugins.push(remarkParse);
if (options.useDefaultPlugins) { options.remarkPlugins ??= []; options.remarkPlugins.unshift(...remarkDefaultPlugins); }
// Add remark plugins plugins.push(...options.remarkPlugins);
// Add remark-rehype to generate HAST plugins.push([remarkRehype, options.rehypeOptions ?? {}]);
if (options.sanitize) { // Add rehype-raw to convert raw HTML to HAST plugins.push(rehypeRaw); }
// Add rehype plugins plugins.push(...options.rehypePlugins ?? []);
if (options.sanitize) { // Add rehype-sanitize to make sure HTML is safe plugins.push(rehypeSanitize); // Add rehype-stringify to output HTML ignoring raw HTML nodes plugins.push(rehypeStringify); } else { // Add rehype-stringify to output HTML plugins.push([rehypeStringify, { allowDangerousHtml: true }]); }
const engine = unified.unified();
// Register all plugins // @ts-ignore: let unified take care of loading all the plugins engine.use(plugins);
// Load the pages const remarkEngine = new MarkdownEngine(engine); site.loadPages(options.extensions, { loader, engine: remarkEngine, });
// Register the filter site.filter("md", filter as Helper, true);
async function filter(content: string): Promise<string> { return (await remarkEngine.render(content)).trim(); } };}
lume

Version Info

Tagged at
6 months ago