Skip to content

Commit

Permalink
fix(liquid): Process large fence blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
3y3k0 authored and 3y3 committed Feb 7, 2023
1 parent 0f5c13f commit 0e97260
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
68 changes: 48 additions & 20 deletions src/transform/liquid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,64 @@ import ArgvService, {ArgvSettings} from './services/argv';
import {Dictionary} from 'lodash';

const codes: string[] = [];
const regexp = /`{3}(((?!`{3})[^])+)`{3}/g;

const fence = '```';

const find = (open: string, close: string, string: string, index: number) => {
const start = string.indexOf(open, index);
const end = start > -1 ? string.indexOf(close, start + open.length) : -1;

return [start, end];
};

const replace = (
open: string,
close: string,
value: (string: string) => string,
string: string,
) => {
let result = '';
let carriage = 0;
let [start, end] = find(open, close, string, carriage);

while (start > -1 && end > -1) {
const fragment = string.slice(start + open.length, end);

result += string.slice(carriage, start) + open + value(fragment) + close;
carriage = end + close.length;
[start, end] = find(open, close, string, carriage);
}

result += string.slice(carriage);

return result;
};

function saveCode(
str: string,
vars: Record<string, unknown>,
path?: string,
substitutions?: boolean,
) {
let i = 0;

return str.replace(regexp, (_, code: string) => {
i++;

const codeWithVars = substitutions ? applySubstitutions(code, vars, path) : code;

codes.push(codeWithVars);

/* Keep the same count of lines to avoid transformation of the source map */
const codeLines = codeWithVars.split('\n');
const emptyLines = codeLines.length > 1 ? codeLines.reduce((acc) => acc + '\n', '') : '';

return '```' + i + emptyLines + '```';
});
return replace(
fence,
fence,
(code) => {
const codeWithVars = substitutions ? applySubstitutions(code, vars, path) : code;
const index = codes.push(codeWithVars) - 1;

/* Keep the same count of lines to avoid transformation of the source map */
const codeLines = codeWithVars.split('\n');
const emptyLines = codeLines.length > 1 ? '\n'.repeat(codeLines.length) : '';

return `${index}${emptyLines}`;
},
str,
);
}

function repairCode(str: string) {
return str.replace(regexp, (_, code) => {
const number = code.trimRight();
return '```' + codes[number - 1] + '```';
});
return replace(fence, fence, (code) => codes[Number(code)], str);
}

function liquid<
Expand Down
5 changes: 4 additions & 1 deletion src/transform/plugins/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ export interface MarkdownItPluginOpts {
isLintRun: boolean;
}

export type MarkdownItPluginCb<T = {}> = (md: MarkdownIt, opts: T & MarkdownItPluginOpts) => void;
export type MarkdownItPluginCb<T extends {} = {}> = (
md: MarkdownIt,
opts: T & MarkdownItPluginOpts,
) => void;

0 comments on commit 0e97260

Please sign in to comment.