({ katex = null, markdownItTexmath = null } = {})
| 147 | } |
| 148 | |
| 149 | function createMarkdownIt({ katex = null, markdownItTexmath = null } = {}) { |
| 150 | const md = markdownIt({ |
| 151 | html: true, |
| 152 | linkify: true, |
| 153 | }); |
| 154 | |
| 155 | md.use(MarkdownItGitHubAlerts) |
| 156 | .use(anchor, { slugify }) |
| 157 | .use(markdownItTaskLists) |
| 158 | .use(markdownItFootnote); |
| 159 | |
| 160 | if (katex && markdownItTexmath) { |
| 161 | md.use(markdownItTexmath, { |
| 162 | engine: katex, |
| 163 | delimiters: ["dollars", "beg_end"], |
| 164 | katexOptions: { |
| 165 | throwOnError: false, |
| 166 | strict: "ignore", |
| 167 | }, |
| 168 | }); |
| 169 | } |
| 170 | |
| 171 | md.use(markdownItEmoji); |
| 172 | |
| 173 | md.renderer.rules.image = (tokens, idx, options, env, self) => { |
| 174 | const token = tokens[idx]; |
| 175 | token.attrSet("loading", "lazy"); |
| 176 | token.attrSet("decoding", "async"); |
| 177 | |
| 178 | const src = token.attrGet("src"); |
| 179 | if (src && !src.startsWith("data:") && !isExternalLink(src)) { |
| 180 | const resolvedPath = resolveImageTarget(src, env.markdownBaseUri); |
| 181 | if (resolvedPath) { |
| 182 | token.attrSet("data-markdown-local-src", resolvedPath); |
| 183 | token.attrSet("src", IMAGE_PLACEHOLDER); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | return self.renderToken(tokens, idx, options); |
| 188 | }; |
| 189 | |
| 190 | md.renderer.rules.fence = (tokens, idx) => { |
| 191 | const token = tokens[idx]; |
| 192 | const info = (token.info || "").trim(); |
| 193 | const language = info.split(/\s+/)[0].toLowerCase(); |
| 194 | const escapedCode = md.utils.escapeHtml(token.content || ""); |
| 195 | |
| 196 | if (language === "mermaid") { |
| 197 | return `<div class="mermaid">${escapedCode}</div>`; |
| 198 | } |
| 199 | |
| 200 | const className = language |
| 201 | ? ` class="language-${escapeAttribute(language)}"` |
| 202 | : ""; |
| 203 | const dataLanguage = ` data-language="${escapeAttribute(language)}"`; |
| 204 | |
| 205 | return `<pre><code${className}${dataLanguage}>${escapedCode}</code></pre>`; |
| 206 | }; |
no test coverage detected