Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/rules.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
const noopTest = { exec: () => null } as unknown as RegExp;

function cachedIndentRegex(createRegex: (indent: number) => RegExp) {
const cache: RegExp[] = [];
return (indent: number) => {
const cappedIndent = Math.max(0, Math.min(3, indent - 1));
const cacheIndex = cappedIndent;
let regex = cache[cacheIndex];
if (!regex) {
regex = createRegex(cappedIndent);
cache[cacheIndex] = regex;
}
return regex;
Comment on lines +6 to +13
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const cappedIndent = Math.max(0, Math.min(3, indent - 1));
const cacheIndex = cappedIndent;
let regex = cache[cacheIndex];
if (!regex) {
regex = createRegex(cappedIndent);
cache[cacheIndex] = regex;
}
return regex;
const cacheIndex = Math.max(0, Math.min(3, indent - 1));
let regex = cache[cacheIndex];
if (!regex) {
regex = createRegex(cacheIndex);
cache[cacheIndex] = regex;
}
return regex;

Seems like you could eliminate one of these variables since the indent and index are the same value.

};
}

function edit(regex: string | RegExp, opt = '') {
let source = typeof regex === 'string' ? regex : regex.source;
const obj = {
Expand Down Expand Up @@ -78,12 +92,12 @@ export const other = {
notSpaceStart: /^\S*/,
endingNewline: /\n$/,
listItemRegex: (bull: string) => new RegExp(`^( {0,3}${bull})((?:[\t ][^\\n]*)?(?:\\n|$))`),
nextBulletRegex: (indent: number) => new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:[*+-]|\\d{1,9}[.)])((?:[ \t][^\\n]*)?(?:\\n|$))`),
hrRegex: (indent: number) => new RegExp(`^ {0,${Math.min(3, indent - 1)}}((?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$)`),
fencesBeginRegex: (indent: number) => new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:\`\`\`|~~~)`),
headingBeginRegex: (indent: number) => new RegExp(`^ {0,${Math.min(3, indent - 1)}}#`),
htmlBeginRegex: (indent: number) => new RegExp(`^ {0,${Math.min(3, indent - 1)}}<(?:[a-z].*>|!--)`, 'i'),
blockquoteBeginRegex: (indent: number) => new RegExp(`^ {0,${Math.min(3, indent - 1)}}>`),
nextBulletRegex: cachedIndentRegex((indent: number) => new RegExp(`^ {0,${indent}}(?:[*+-]|\\d{1,9}[.)])((?:[ \t][^\\n]*)?(?:\\n|$))`)),
hrRegex: cachedIndentRegex((indent: number) => new RegExp(`^ {0,${indent}}((?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$)`)),
fencesBeginRegex: cachedIndentRegex((indent: number) => new RegExp(`^ {0,${indent}}(?:\`\`\`|~~~)`)),
headingBeginRegex: cachedIndentRegex((indent: number) => new RegExp(`^ {0,${indent}}#`)),
htmlBeginRegex: cachedIndentRegex((indent: number) => new RegExp(`^ {0,${indent}}<(?:[a-z].*>|!--)`, 'i')),
blockquoteBeginRegex: cachedIndentRegex((indent: number) => new RegExp(`^ {0,${indent}}>`)),
};

/**
Expand Down
20 changes: 20 additions & 0 deletions test/unit/Lexer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,26 @@ function expectInlineTokens({ md, options, tokens, links = {} }) {
}

describe('Lexer', () => {
describe('rules', () => {
it('should create indentation regexes for zero indent', () => {
const {
nextBulletRegex,
hrRegex,
fencesBeginRegex,
headingBeginRegex,
htmlBeginRegex,
blockquoteBeginRegex,
} = new Lexer().tokenizer.rules.other;

assert.ok(nextBulletRegex(0).test('- item\n'));
assert.ok(hrRegex(0).test('---\n'));
assert.ok(fencesBeginRegex(0).test('```'));
assert.ok(headingBeginRegex(0).test('# heading'));
assert.ok(htmlBeginRegex(0).test('<div>'));
assert.ok(blockquoteBeginRegex(0).test('> quote'));
});
});

describe('paragraph', () => {
it('space between paragraphs', () => {
expectTokens({
Expand Down