|
| 1 | +import type {OptionsType, OutputType, EnvType} from './typings'; |
1 | 2 | import {bold} from 'chalk';
|
2 |
| -import attrs from 'markdown-it-attrs'; |
3 |
| -import Token from 'markdown-it/lib/token'; |
4 |
| - |
5 |
| -import {log, LogLevels} from './log'; |
6 |
| -import makeHighlight from './highlight'; |
7 |
| -import extractTitle from './title'; |
8 |
| -import getHeadings from './headings'; |
| 3 | +import {log} from './log'; |
9 | 4 | import liquid from './liquid';
|
10 |
| -import sanitizeHtml, {SanitizeOptions} from './sanitize'; |
11 |
| - |
12 |
| -import notes from './plugins/notes'; |
13 |
| -import anchors from './plugins/anchors'; |
14 |
| -import code from './plugins/code'; |
15 |
| -import cut from './plugins/cut'; |
16 |
| -import deflist from './plugins/deflist'; |
17 |
| -import term from './plugins/term'; |
18 |
| -import file from './plugins/file'; |
19 |
| -import imsize from './plugins/imsize'; |
20 |
| -import meta from './plugins/meta'; |
21 |
| -import sup from './plugins/sup'; |
22 |
| -import tabs from './plugins/tabs'; |
23 |
| -import video from './plugins/video'; |
24 |
| -import monospace from './plugins/monospace'; |
25 |
| -import yfmTable from './plugins/table'; |
26 |
| -import {initMd} from './md'; |
27 |
| -import {MarkdownItPluginCb} from './plugins/typings'; |
28 |
| -import type {HighlightLangMap, Heading} from './typings'; |
29 |
| - |
30 |
| -interface OutputType { |
31 |
| - result: { |
32 |
| - html: string; |
33 |
| - title?: string; |
34 |
| - headings: Heading[]; |
35 |
| - assets?: unknown[]; |
36 |
| - meta?: object; |
37 |
| - }; |
38 |
| - logs: Record<LogLevels, string[]>; |
39 |
| -} |
40 |
| -interface OptionsType { |
41 |
| - vars?: Record<string, string>; |
42 |
| - path?: string; |
43 |
| - extractTitle?: boolean; |
44 |
| - needTitle?: boolean; |
45 |
| - allowHTML?: boolean; |
46 |
| - linkify?: boolean; |
47 |
| - linkifyTlds?: string | string[]; |
48 |
| - breaks?: boolean; |
49 |
| - conditionsInCode?: boolean; |
50 |
| - disableLiquid?: boolean; |
51 |
| - leftDelimiter?: string; |
52 |
| - rightDelimiter?: string; |
53 |
| - isLiquided?: boolean; |
54 |
| - needToSanitizeHtml?: boolean; |
55 |
| - sanitizeOptions?: SanitizeOptions; |
56 |
| - needFlatListHeadings?: boolean; |
57 |
| - // eslint-disable-next-line @typescript-eslint/no-explicit-any |
58 |
| - plugins?: MarkdownItPluginCb<any>[]; |
59 |
| - highlightLangs?: HighlightLangMap; |
60 |
| - root?: string; |
61 |
| - [x: string]: unknown; |
62 |
| -} |
| 5 | +import initMarkdownit from './md'; |
63 | 6 |
|
64 |
| -function transform(originInput: string, opts: OptionsType = {}): OutputType { |
| 7 | +function applyLiquid(input: string, options: OptionsType) { |
65 | 8 | const {
|
66 | 9 | vars = {},
|
67 | 10 | path,
|
68 |
| - extractTitle: extractTitleOption, |
69 |
| - needTitle, |
70 |
| - allowHTML = false, |
71 |
| - linkify = false, |
72 |
| - linkifyTlds, |
73 |
| - breaks = true, |
74 | 11 | conditionsInCode = false,
|
75 |
| - needToSanitizeHtml = false, |
76 |
| - sanitizeOptions, |
77 |
| - needFlatListHeadings = false, |
78 | 12 | disableLiquid = false,
|
79 |
| - leftDelimiter = '{', |
80 |
| - rightDelimiter = '}', |
81 | 13 | isLiquided = false,
|
82 |
| - plugins = [ |
83 |
| - meta, |
84 |
| - deflist, |
85 |
| - cut, |
86 |
| - notes, |
87 |
| - anchors, |
88 |
| - tabs, |
89 |
| - code, |
90 |
| - sup, |
91 |
| - video, |
92 |
| - monospace, |
93 |
| - yfmTable, |
94 |
| - file, |
95 |
| - imsize, |
96 |
| - term, |
97 |
| - ], |
98 |
| - highlightLangs = {}, |
99 |
| - ...customOptions |
100 |
| - } = opts; |
| 14 | + } = options; |
101 | 15 |
|
102 |
| - const pluginOptions = { |
103 |
| - ...customOptions, |
104 |
| - conditionsInCode, |
105 |
| - vars, |
106 |
| - path, |
107 |
| - extractTitle: extractTitleOption, |
108 |
| - disableLiquid, |
109 |
| - log, |
110 |
| - }; |
| 16 | + return disableLiquid || isLiquided ? input : liquid(input, vars, path, {conditionsInCode}); |
| 17 | +} |
111 | 18 |
|
112 |
| - const input = |
113 |
| - disableLiquid || isLiquided |
114 |
| - ? originInput |
115 |
| - : liquid(originInput, vars, path, {conditionsInCode}); |
| 19 | +function handleError(error: unknown, path?: string): never { |
| 20 | + log.error(`Error occurred${path ? ` in ${bold(path)}` : ''}`); |
116 | 21 |
|
117 |
| - const highlight = makeHighlight(highlightLangs); |
118 |
| - const md = initMd({html: allowHTML, linkify, highlight, breaks}); |
119 |
| - // Need for ids of headers |
120 |
| - md.use(attrs, {leftDelimiter, rightDelimiter}); |
| 22 | + throw error; |
| 23 | +} |
121 | 24 |
|
122 |
| - plugins.forEach((plugin) => md.use(plugin, pluginOptions)); |
| 25 | +function emitResult(html: string, env: EnvType): OutputType { |
| 26 | + return { |
| 27 | + result: {...env, html}, |
| 28 | + logs: log.get(), |
| 29 | + }; |
| 30 | +} |
123 | 31 |
|
124 |
| - if (linkify && linkifyTlds) { |
125 |
| - md.linkify.tlds(linkifyTlds, true); |
126 |
| - } |
| 32 | +// eslint-disable-next-line consistent-return |
| 33 | +function transform(originInput: string, options: OptionsType = {}) { |
| 34 | + const input = applyLiquid(originInput, options); |
| 35 | + const {parse, compile, env} = initMarkdownit(options); |
127 | 36 |
|
128 | 37 | try {
|
129 |
| - let title; |
130 |
| - let tokens; |
131 |
| - let titleTokens; |
132 |
| - const env = {} as {[key: string]: Token[] | unknown}; |
133 |
| - |
134 |
| - tokens = md.parse(input, env); |
135 |
| - |
136 |
| - if (extractTitleOption) { |
137 |
| - ({title, tokens, titleTokens} = extractTitle(tokens)); |
138 |
| - |
139 |
| - // title tokens include other tokens that need to be transformed |
140 |
| - if (titleTokens.length > 1) { |
141 |
| - title = md.renderer.render(titleTokens, md.options, env); |
142 |
| - } |
143 |
| - } |
144 |
| - if (needTitle) { |
145 |
| - ({title} = extractTitle(tokens)); |
146 |
| - } |
147 |
| - |
148 |
| - const headings = getHeadings(tokens, needFlatListHeadings); |
149 |
| - |
150 |
| - // add all term template tokens to the end of the html |
151 |
| - const termTokens = (env.termTokens as Token[]) || []; |
152 |
| - let html = md.renderer.render([...tokens, ...termTokens], md.options, env); |
153 |
| - if (needToSanitizeHtml) { |
154 |
| - html = sanitizeHtml(html, sanitizeOptions); |
155 |
| - } |
156 |
| - |
157 |
| - const assets = md.assets; |
158 |
| - const meta = md.meta; |
159 |
| - |
160 |
| - return { |
161 |
| - result: {html, title, headings, assets, meta}, |
162 |
| - logs: log.get(), |
163 |
| - }; |
164 |
| - } catch (err) { |
165 |
| - log.error(`Error occurred${path ? ` in ${bold(path)}` : ''}`); |
166 |
| - throw err; |
| 38 | + return emitResult(compile(parse(input)), env); |
| 39 | + } catch (error) { |
| 40 | + handleError(error, options.path); |
167 | 41 | }
|
168 | 42 | }
|
169 | 43 |
|
|
0 commit comments