UNPKG

@intlayer/core

Version:

Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.

1 lines 5.67 kB
{"version":3,"file":"parser.mjs","names":[],"sources":["../../../src/markdown/parser.ts"],"sourcesContent":["import { DURATION_DELAY_TRIGGER } from './constants';\nimport type { NestedParser, ParserResult, ParseState, Rules } from './types';\nimport { normalizeWhitespace, qualifies } from './utils';\n\n/**\n * Creates a parser for a given set of rules, with the precedence\n * specified as a list of rules.\n *\n * @param rules - An object containing rule type -> {match, order, parse} objects\n * (lower order is higher precedence)\n *\n * @returns The resulting parse function\n */\nexport const parserFor = (\n rules: Rules\n): ((source: string, state: ParseState) => ParserResult[]) => {\n const start = performance.now();\n const ruleList = Object.keys(rules);\n\n if (process.env.NODE_ENV !== 'production') {\n ruleList.forEach((type) => {\n const order = rules[type]?._order;\n if (typeof order !== 'number' || !Number.isFinite(order)) {\n console.warn(`intlayer: Invalid order for rule \\`${type}\\`: ${order}`);\n }\n });\n }\n\n // Sorts rules in order of increasing order, then\n // ascending rule name (numeric) in case of ties.\n // RuleType keys are string numbers — use numeric comparison to preserve\n // intended ordering (e.g. codeFenced '4' must precede headingSetext '10').\n ruleList.sort((a, b) => {\n return rules[a]!._order - rules[b]!._order || +a - +b;\n });\n\n const nestedParse: NestedParser = (\n source: string,\n state: ParseState = {}\n ): ParserResult[] => {\n const parseStart = performance.now();\n const result: ParserResult[] = [];\n state.prevCapture = state.prevCapture || '';\n\n if (source.trim()) {\n while (source) {\n let i = 0;\n while (i < ruleList.length) {\n const ruleType = ruleList[i];\n const rule = rules[ruleType as keyof typeof rules];\n\n if (rule?._qualify && !qualifies(source, state, rule._qualify)) {\n i++;\n continue;\n }\n\n const matchStart = performance.now();\n const capture = rule?._match(source, state);\n\n if (process.env.NODE_ENV !== 'production') {\n const matchDuration = performance.now() - matchStart;\n if (matchDuration > 1) {\n console.log(\n `${ruleType}._match: ${matchDuration.toFixed(3)}ms, source length: ${source.length}`\n );\n }\n }\n\n if (capture?.[0]) {\n source = source.substring(capture[0].length);\n\n const ruleParseStart = performance.now();\n const parsedAny: any = rule?._parse(capture, nestedParse, state);\n\n if (process.env.NODE_ENV !== 'production') {\n const ruleParseDuration = performance.now() - ruleParseStart;\n if (ruleParseDuration > 1) {\n console.log(\n `${ruleType}._parse: ${ruleParseDuration.toFixed(3)}ms, capture length: ${capture[0].length}`\n );\n }\n }\n\n state.prevCapture = (state.prevCapture || '') + capture[0];\n\n if (!parsedAny.type) {\n parsedAny.type = ruleType;\n }\n result.push(parsedAny as ParserResult);\n break;\n }\n i++;\n }\n }\n }\n\n // Tree shakeable log for prod\n if (process.env.NODE_ENV === 'development') {\n const parseDuration = performance.now() - parseStart;\n if (parseDuration > 1) {\n console.log(\n `nestedParse: ${parseDuration.toFixed(3)}ms, source length: ${source.length}, result count: ${result.length}`\n );\n }\n }\n\n return result;\n };\n\n // Tree shakeable log for prod\n if (process.env.NODE_ENV === 'development') {\n const duration = performance.now() - start;\n\n if (duration > DURATION_DELAY_TRIGGER) {\n console.log(\n `parserFor: ${duration.toFixed(3)}ms, rules count: ${ruleList.length}`\n );\n }\n }\n\n return (source: string, state: ParseState) =>\n nestedParse(normalizeWhitespace(source), state);\n};\n"],"mappings":";;;;;;;;;;;;;AAaA,MAAa,aACX,UAC4D;CAC5D,MAAM,QAAQ,YAAY,KAAK;CAC/B,MAAM,WAAW,OAAO,KAAK,MAAM;AAGjC,UAAS,SAAS,SAAS;EACzB,MAAM,QAAQ,MAAM,OAAO;AAC3B,MAAI,OAAO,UAAU,YAAY,CAAC,OAAO,SAAS,MAAM,CACtD,SAAQ,KAAK,sCAAsC,KAAK,MAAM,QAAQ;GAExE;AAOJ,UAAS,MAAM,GAAG,MAAM;AACtB,SAAO,MAAM,GAAI,SAAS,MAAM,GAAI,UAAU,CAAC,IAAI,CAAC;GACpD;CAEF,MAAM,eACJ,QACA,QAAoB,EAAE,KACH;EACnB,MAAM,aAAa,YAAY,KAAK;EACpC,MAAM,SAAyB,EAAE;AACjC,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