/
githubmirror
/
marktext
Обзор
Документация
Войти
/
githubmirror
/
marktext
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
packages/muya/src/utils/marked/lexBlock.ts
56 строк
2 KB
Ran Luo
feat(muya): migrate TS rewrite to packages/muya alongside legacy muyajs (#4314)
29 май 2026, 15:35
Не верифицирован
29 май 2026, 15:35
0d86641
Код
Авторство
О чём код?
import type { Token } from 'marked'; import type { IFrontmatterToken, ILexOption, TLexedToken } from './types'; import { Marked } from 'marked'; import compatibleTaskList from './compatibleTaskList'; import footnoteExtension from './extensions/footnote'; import mathExtension from './extensions/math'; import fm from './frontMatter'; import { DEFAULT_OPTIONS } from './options'; import walkTokens from './walkTokens'; export function lexBlock( src: string, options: ILexOption = DEFAULT_OPTIONS, ): TLexedToken[] { options = Object.assign({}, DEFAULT_OPTIONS, options); const { math, frontMatter, footnote } = options; let tokens: (Token | IFrontmatterToken)[] = []; // Use a per-call Marked instance so extensions don't bleed across calls. // marked.use() on the global singleton would make math / footnote sticky: // any consumer that once passed `math: true` would get math parsing forever. const m = new Marked(); if (math) { m.use( mathExtension({ throwOnError: false, useKatexRender: false, }), ); } if (footnote) { m.use(footnoteExtension()); } if (frontMatter) { const { token, src: newSrc } = fm(src); if (token) { tokens.push(token); src = newSrc; } } // Pass `m.defaults` to the Lexer so the extensions registered via m.use() // are picked up; the no-arg constructor would fall back to global defaults. tokens.push(...new m.Lexer(m.defaults).blockTokens(src)); tokens = compatibleTaskList(tokens as Token[]); m.walkTokens(tokens as Token[], walkTokens(options)); // After walkTokens / compatibleTaskList run, marked's Heading/List/ListItem // tokens have been augmented with muya-specific fields (headingStyle, // marker, listType, listItemType, bulletMarkerOrDelimiter). The wider // TLexedToken union captures that runtime shape. return tokens as TLexedToken[]; }