Skip to content

Commit

Permalink
refactor(parser): refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
falsandtru committed Dec 9, 2017
1 parent fc60290 commit cb87e06
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/parser/block/extension/placeholder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface PlaceholderParser extends
Markdown<'extensionblock' & 'extensionblock/placeholder'>,
Parser<HTMLElement, never[]> {
}
const syntax = /^(~{3,})([^\n]*)\n(?:[^\n]*\n)*?\1[^\S\n]*(?=\n|$)/;
const syntax = /^(~{3,})([^\n]*)\n(?:[^\n]*\n)*?\1[^\S\n]*(?:\n|$)/;

export const placeholder: PlaceholderParser = verify((source: string): [HTMLElement[], string] | undefined => {
if (!source.startsWith('~~~')) return;
Expand All @@ -28,5 +28,5 @@ export const placeholder: PlaceholderParser = verify((source: string): [HTMLElem
}
const quote = document.createElement('pre');
void quote.appendChild(document.createTextNode(`${keyword}${notes}\n${lines.join('')}${keyword}`));
return [[message, quote], source.slice(keyword.length + 1)];
return [[message, quote], source.slice(source.split('\n', 1)[0].length + 1)];
});
2 changes: 1 addition & 1 deletion src/parser/block/horizontalrule.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HorizontalRuleParser } from '../block';
import { verify } from './util/verification';

const syntax = /^\s*-\s*-\s*(?:-\s*)+(?:\n|$)/;
const syntax = /^(?:\s*-){3,}\s*$/;

export const horizontalrule: HorizontalRuleParser = verify((source: string): [[HTMLHRElement], string] | undefined => {
const [whole = ''] = source.split('\n', 1)[0].match(syntax) || [];
Expand Down
4 changes: 2 additions & 2 deletions src/parser/block/mathblock.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MathBlockParser } from '../block';
import { verify } from './util/verification';

const syntax = /^\$\$[^\S\n]*\n(?:[^\n]*?\S[^\n]*\n)+?\$\$[^\S\n]*(?=\n|$)/;
const syntax = /^\$\$[^\S\n]*\n(?:[^\n]*?\S[^\n]*\n)+?\$\$[^\S\n]*(?:\n|$)/;

export const mathblock: MathBlockParser = verify((source: string): [[HTMLDivElement], string] | undefined => {
if (!source.startsWith('$$')) return;
Expand All @@ -10,5 +10,5 @@ export const mathblock: MathBlockParser = verify((source: string): [[HTMLDivElem
const el = document.createElement('div');
void el.setAttribute('class', 'math');
void el.appendChild(document.createTextNode(whole));
return [[el], source.slice(whole.length + 1)];
return [[el], source.slice(whole.length)];
});
4 changes: 2 additions & 2 deletions src/parser/block/newline.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { newline } from './newline';
import { loop } from '../../combinator';
import { inspect } from '../../debug.test';

describe('Unit: parser/block/newline', () => {
describe('newline', () => {
const parser = loop(newline);
const parser = newline;

it('invalid', () => {
assert.deepStrictEqual(inspect(parser('')), undefined);
Expand All @@ -20,6 +19,7 @@ describe('Unit: parser/block/newline', () => {
assert.deepStrictEqual(inspect(parser('\n\n\n')), [[], '']);
assert.deepStrictEqual(inspect(parser('\n\n\na')), [[], 'a']);
assert.deepStrictEqual(inspect(parser('\\\n')), [[], '']);
assert.deepStrictEqual(inspect(parser(' \n \\\n')), [[], '']);
});

});
Expand Down
2 changes: 1 addition & 1 deletion src/parser/block/newline.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NewlineParser } from '../block';

const syntax = /^[^\S\n]*?\\?\n/;
const syntax = /^(?:[^\S\n]*?\\?\n)+/;

export const newline: NewlineParser = (source: string): [never[], string] | undefined => {
const [whole = ''] = source.match(syntax) || [];
Expand Down
4 changes: 2 additions & 2 deletions src/parser/block/pretext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { loop } from '../../combinator';
import { escsource } from '../source/escapable';
import { squash } from '../squash';

const syntax = /^(`{3,})([^\n]*)\n(?:[^\n]*\n)+?\1[^\S\n]*(?=\n|$)/;
const syntax = /^(`{3,})([^\n]*)\n(?:[^\n]*\n)+?\1[^\S\n]*(?:\n|$)/;

export const pretext: PretextParser = verify((source: string): [[HTMLPreElement], string] | undefined => {
if (!source.startsWith('```')) return;
Expand All @@ -21,5 +21,5 @@ export const pretext: PretextParser = verify((source: string): [[HTMLPreElement]
void el.setAttribute('data-file', filename);
}
void el.appendChild(document.createTextNode(whole.slice(whole.indexOf('\n') + 1, whole.lastIndexOf('\n'))));
return [[el], source.slice(whole.length + 1)];
return [[el], source.slice(whole.length)];
});

0 comments on commit cb87e06

Please sign in to comment.