|
1 | 1 | import * as Prism from 'prismjs'; |
| 2 | +import { escapeHtml } from '../render/utils.js'; |
2 | 3 | /** |
3 | 4 | * |
4 | 5 | * The dependencies map which syncs from |
@@ -220,6 +221,28 @@ const lang_aliases = { |
220 | 221 | // preventing duplicate calculations and avoiding repeated warning messages. |
221 | 222 | const depTreeCache = {}; |
222 | 223 |
|
| 224 | +/** |
| 225 | + * Normalizes the declared code-block language and provides a safe HTML value. |
| 226 | + * |
| 227 | + * - `codeLang`: normalized user-declared language (fallback: `markup`) |
| 228 | + * - `prismLang`: resolved Prism language key used for dependency/highlight lookup |
| 229 | + * - `escapedLang`: escaped language for safe insertion into HTML attributes |
| 230 | + * |
| 231 | + * @param {*} lang |
| 232 | + * @returns {{codeLang: string, prismLang: any|string, escapedLang: string}} |
| 233 | + */ |
| 234 | +export const sanitizeCodeLang = lang => { |
| 235 | + const codeLang = |
| 236 | + typeof lang === 'string' && lang.trim().length ? lang.trim() : 'markup'; |
| 237 | + const prismLang = lang_aliases[codeLang] || codeLang; |
| 238 | + |
| 239 | + return { |
| 240 | + codeLang, |
| 241 | + prismLang, |
| 242 | + escapedLang: escapeHtml(codeLang), |
| 243 | + }; |
| 244 | +}; |
| 245 | + |
223 | 246 | /** |
224 | 247 | * PrismJs language dependencies required a specific order to load. |
225 | 248 | * Try to check and print a warning message if some dependencies missing or in wrong order. |
@@ -254,11 +277,11 @@ export default function checkLangDependenciesAllLoaded(lang) { |
254 | 277 | depTreeCache[lang] = depTree; |
255 | 278 |
|
256 | 279 | if (!dummy.loaded) { |
257 | | - const prettyOutput = prettryPrint(depTree, 1); |
| 280 | + const prettyOutput = prettyPrint(depTree, 1); |
258 | 281 | // eslint-disable-next-line no-console |
259 | 282 | console.warn( |
260 | 283 | `The language '${lang}' required dependencies for code block highlighting are not satisfied.`, |
261 | | - `Priority dependencies from low to high, consider to place all the necessary dependencie by priority (higher first): \n`, |
| 284 | + `Priority dependencies from low to high, consider to place all the necessary dependencies by priority (higher first): \n`, |
262 | 285 | prettyOutput, |
263 | 286 | ); |
264 | 287 | } |
@@ -288,11 +311,11 @@ const buildAndCheckDepTree = (lang, parent, dummy) => { |
288 | 311 | parent.dependencies.push(cur); |
289 | 312 | }; |
290 | 313 |
|
291 | | -const prettryPrint = (depTree, level) => { |
| 314 | +const prettyPrint = (depTree, level) => { |
292 | 315 | let cur = `${' '.repeat(level * 3)} ${depTree.cur} ${depTree.loaded ? '(+)' : '(-)'}`; |
293 | 316 | if (depTree.dependencies.length) { |
294 | 317 | depTree.dependencies.forEach(dep => { |
295 | | - cur += prettryPrint(dep, level + 1); |
| 318 | + cur += prettyPrint(dep, level + 1); |
296 | 319 | }); |
297 | 320 | } |
298 | 321 | return '\n' + cur; |
|
0 commit comments