diff --git a/src/transform/md.ts b/src/transform/md.ts index e3c9375e..3bc4872e 100644 --- a/src/transform/md.ts +++ b/src/transform/md.ts @@ -5,7 +5,6 @@ import DefaultMarkdownIt from 'markdown-it'; import attrs from 'markdown-it-attrs'; import DefaultPlugins from './plugins'; -import {preprocess} from './preprocessors'; import {log} from './log'; import makeHighlight from './highlight'; import extractTitle from './title'; @@ -60,7 +59,7 @@ function initMarkdownIt(options: OptionsType) { initPlugins(md, options, pluginOptions); // Init preprocessor and MD parser - const parse = initParser(md, options, env, pluginOptions); + const parse = initParser(md, options, env); // Init render to HTML compiler const compile = initCompiler(md, options, env); @@ -116,12 +115,7 @@ function initPlugins(md: MarkdownIt, options: OptionsType, pluginOptions: Markdo } } -function initParser( - md: MarkdownIt, - options: OptionsType, - env: EnvType, - pluginOptions: MarkdownItPluginOpts, -) { +function initParser(md: MarkdownIt, options: OptionsType, env: EnvType) { return (input: string) => { const { extractTitle: extractTitleOption, @@ -130,9 +124,6 @@ function initParser( getPublicPath, } = options; - // Run preprocessor - input = preprocess(input, pluginOptions, options, md); - // Generate global href link const href = getPublicPath ? getPublicPath(options) : ''; diff --git a/src/transform/preprocessors/included/index.ts b/src/transform/preprocessors/included/index.ts deleted file mode 100644 index d2953f48..00000000 --- a/src/transform/preprocessors/included/index.ts +++ /dev/null @@ -1,107 +0,0 @@ -import {getFullIncludePath} from '../../utilsFS'; -import {MarkdownItPreprocessorCb} from '../../typings'; -import {MarkdownItIncluded} from '../../plugins/includes/types'; - -const INCLUDE_REGEXP = /^\s*{%\s*included\s*\((.+?)\)\s*%}\s*$/; -const INCLUDE_END_REGEXP = /^\s*{% endincluded %}\s*$/; - -const preprocessLine = ( - lines: string[], - start: number, - { - root, - path, - }: { - root?: string; - path?: string; - }, - md?: MarkdownItIncluded, -) => { - const hasIncludedCache = md && root && path; - const str = lines[start]; - const match = str?.match(INCLUDE_REGEXP); - - // Protect from unmatched results - if (!match) { - return false; - } - - const includePathKey = match[1]; - - // Protect from empty path - if (!includePathKey) { - return false; - } - - const includePaths = includePathKey.split(':'); - - // Read all content from top to bottom(!) char of the included block - const data = []; - let line = start; - while (line < lines.length) { - line++; - const str = lines[line]; - if (str === null) { - break; - } - if (str?.match(INCLUDE_END_REGEXP)) { - break; - } - data.push(str); - } - - // No included cache for lint mode - if (hasIncludedCache) { - if (!md.included) { - md.included = {}; - } - - // Normalize the path to absolute - let includePath = getFullIncludePath(includePaths[0], root, path); - for (let index = 1; index < includePaths.length; index++) { - const pathname = includePaths[index]; - includePath = getFullIncludePath(pathname, root, includePath); - } - - // Store the included content - md.included[includePath] = data.join('\n'); - } - - // Remove the content of the included file - lines.splice(start, data.length + 2); - - return true; -}; - -const index: MarkdownItPreprocessorCb<{ - included?: boolean; -}> = (input, options, md?: MarkdownItIncluded) => { - const {included, path, root} = options; - - // To reduce file reading we can include the file content into the generated content - if (included !== false) { - const lines = input?.split('\n') || []; - - // The finction reads the files from bottom to top(!). It stops the loop if it does not have anything to swap. - // If the function finds something to process then it restarts the loop because the position of the last element has been moved. - // eslint-disable-next-line no-unmodified-loop-condition - while (input?.length) { - let hasChars = false; - for (let line = lines.length - 1; line >= 0; line--) { - hasChars = preprocessLine(lines, line, {path, root}, md); - if (hasChars) { - break; - } - } - if (!hasChars) { - break; - } - } - - input = lines.join('\n'); - } - - return input; -}; - -export = index; diff --git a/src/transform/preprocessors/index.ts b/src/transform/preprocessors/index.ts deleted file mode 100644 index 9b60aa50..00000000 --- a/src/transform/preprocessors/index.ts +++ /dev/null @@ -1,25 +0,0 @@ -import type {MarkdownIt, MarkdownItPluginOpts, MarkdownItPreprocessorCb} from '../plugins/typings'; -import type {PluginOptions} from '../yfmlint'; - -import included from './included'; - -const defaultPreprocessors = [included] as MarkdownItPreprocessorCb[]; - -export default defaultPreprocessors; - -export function preprocess( - content: string, - pluginOptions: MarkdownItPluginOpts | PluginOptions | unknown, - options?: Partial & { - preprocessors?: MarkdownItPreprocessorCb[]; - }, - md?: MarkdownIt, -) { - const {preprocessors = defaultPreprocessors} = options ?? {}; - - for (const preprocessor of preprocessors) { - content = preprocessor(content, pluginOptions as MarkdownItPluginOpts, md); - } - - return content; -} diff --git a/src/transform/typings.ts b/src/transform/typings.ts index 3cb590cf..ce67c24e 100644 --- a/src/transform/typings.ts +++ b/src/transform/typings.ts @@ -68,7 +68,6 @@ export interface OptionsType { needFlatListHeadings?: boolean; // eslint-disable-next-line @typescript-eslint/no-explicit-any plugins?: ExtendedPluginWithCollect[]; - preprocessors?: MarkdownItPreprocessorCb[]; // Preprocessors should modify the input before passing it to MD highlightLangs?: HighlightLangMap; disableRules?: string[]; extractChangelogs?: boolean; @@ -88,7 +87,7 @@ export interface OptionsType { supportGithubAnchors?: boolean; disableCommonAnchors?: boolean; useCommonAnchorButtons?: boolean; - lang?: Lang; + lang?: string; [x: string]: unknown; } diff --git a/src/transform/utilsFS.ts b/src/transform/utilsFS.ts index 05084f3e..fe7dd61f 100644 --- a/src/transform/utilsFS.ts +++ b/src/transform/utilsFS.ts @@ -8,7 +8,6 @@ import QuickLRU from 'quick-lru'; import liquidSnippet from './liquid'; import {StateCore} from './typings'; import {defaultTransformLink} from './utils'; -import {preprocess} from './preprocessors'; const filesCache = new QuickLRU({maxSize: 1000}); @@ -83,9 +82,6 @@ export function getFileTokens( sourceMap = liquidResult.sourceMap; } - // Run preprocessor - content = preprocess(content, options); - if (!disableLint && lintMarkdown) { lintMarkdown({ input: content, diff --git a/src/transform/yfmlint/index.ts b/src/transform/yfmlint/index.ts index d842f05a..1b0f0033 100644 --- a/src/transform/yfmlint/index.ts +++ b/src/transform/yfmlint/index.ts @@ -6,7 +6,6 @@ import union from 'lodash/union'; import attrs from 'markdown-it-attrs'; import {LogLevels, Logger} from '../log'; -import {preprocess} from '../preprocessors'; import baseDefaultLintConfig from './yfmlint'; import { @@ -28,8 +27,7 @@ const defaultLintRules = [yfm001, yfm002, yfm003, yfm004, yfm005, yfm006, yfm007 const lintCache = new Set(); function yfmlint(opts: Options) { - let {input} = opts; - const {plugins: customPlugins, pluginOptions, customLintRules, sourceMap} = opts; + const {input, plugins: customPlugins, pluginOptions, customLintRules, sourceMap} = opts; const {path = 'input', log} = pluginOptions; pluginOptions.isLintRun = true; @@ -61,9 +59,6 @@ function yfmlint(opts: Options) { const plugins = customPlugins && [...(enableMarkdownAttrs ? [attrs] : []), ...customPlugins]; const preparedPlugins = plugins && plugins.map((plugin) => [plugin, pluginOptions]); - // Run preprocessor - input = preprocess(input, pluginOptions, opts); - let result; try { result = sync({