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
29 changes: 28 additions & 1 deletion src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ import type {
CommentNode,
} from './node-types';

const getIndentMatcher = unaryMemoize(
(length: number) => new RegExp(String.raw`\n[\p{Space_Separator}\t]{1,${length}}`, 'gu'),
Array.from({ length: 20 }, (_, i) => i + 1)
);

export class Emitter {
str: string;
indent: number;

constructor() {
this.str = '';
this.indent = 0;
}

emit(node: Node | Node[]) {
Expand Down Expand Up @@ -106,7 +113,10 @@ export class Emitter {
emitListItem(li: OrderedListItemNode | UnorderedListItemNode) {
const attrs = li.attrs.map(a => ` ${a.key}=${JSON.stringify(a.value)}`).join('');
this.str += `<li${attrs}>`;
const oldIndent = this.indent;
this.indent = li.contentsIndent;
this.emitFragment(li.contents);
this.indent = oldIndent;
if (li.sublist !== null) {
if (li.sublist.name === 'ol') {
this.emitOrderedList(li.sublist);
Expand All @@ -130,7 +140,12 @@ export class Emitter {
}

emitText(text: TextNode) {
this.str += text.contents;
let contents = text.contents;
if (this.indent) {
const indentMatcher = getIndentMatcher(this.indent);
contents = contents.replace(indentMatcher, '\n');
}
this.str += contents;
}

emitTick(node: TickNode) {
Expand Down Expand Up @@ -165,3 +180,15 @@ export class Emitter {
this.str += `</${wrapping}>`;
}
}

function unaryMemoize<K, V>(fn: (arg: K) => V, prepopulate: K[] = []) {
const cache = new Map(prepopulate.map(arg => [arg, fn(arg)]));
return (arg: K) => {
let value = cache.get(arg);
if (value === undefined && !cache.has(arg)) {
value = fn(arg);
cache.set(arg, value);
}
return value as V;
};
}
2 changes: 2 additions & 0 deletions src/node-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export type OrderedListNode = {
export type UnorderedListItemNode = {
name: 'unordered-list-item';
contents: FragmentNode[];
contentsIndent: number;
sublist: ListNode | null;
attrs: { key: string; value: string; location: LocationRange }[];
location: LocationRange;
Expand All @@ -196,6 +197,7 @@ export type UnorderedListItemNode = {
export type OrderedListItemNode = {
name: 'ordered-list-item';
contents: FragmentNode[];
contentsIndent: number;
sublist: ListNode | null;
attrs: { key: string; value: string; location: LocationRange }[];
location: LocationRange;
Expand Down
17 changes: 12 additions & 5 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,15 @@ export class Parser {
const startTok = this._t.peek() as OrderedListToken | UnorderedListToken;

let node: Unlocated<ListNode>;
let contentsIndent: number;
if (startTok.name === 'ul') {
const match = startTok.contents.match(/(\s*)\* /);
node = { name: 'ul', indent: match![1].length, contents: [] };
contentsIndent = match![0].length;
} else {
const match = startTok.contents.match(/(\s*)([^.]+)\. /);
node = { name: 'ol', indent: match![1].length, start: Number(match![2]), contents: [] };
contentsIndent = match![0].length;
}

while (true) {
Expand All @@ -101,15 +104,19 @@ export class Parser {
}

// @ts-ignore typescript is not smart enough to figure out that the types line up
node.contents.push(this.parseListItem(node.name, node.indent));
node.contents.push(this.parseListItem(node.name, node.indent, contentsIndent));
}

return this.finish(node);
}

parseListItem(kind: 'ol', indent: number): OrderedListItemNode;
parseListItem(kind: 'ul', indent: number): UnorderedListItemNode;
parseListItem(kind: 'ol' | 'ul', indent: number): OrderedListItemNode | UnorderedListItemNode {
parseListItem(kind: 'ol', indent: number, contentsIndent: number): OrderedListItemNode;
parseListItem(kind: 'ul', indent: number, contentsIndent: number): UnorderedListItemNode;
parseListItem(
kind: 'ol' | 'ul',
indent: number,
contentsIndent: number
): OrderedListItemNode | UnorderedListItemNode {
this.pushPos();
// consume list token
this._t.next();
Expand All @@ -132,7 +139,7 @@ export class Parser {

let name: 'ordered-list-item' | 'unordered-list-item' =
kind === 'ol' ? 'ordered-list-item' : 'unordered-list-item';
return this.finish({ name, contents, sublist, attrs });
return this.finish({ name, contents, contentsIndent, sublist, attrs });
}

parseFragment(opts: ParseFragmentOpts): FragmentNode[];
Expand Down
22 changes: 22 additions & 0 deletions test/cases/list-item-deindenting.raw.ecmarkdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
1. Item 1
1. Item 1a
(multi-line)
1. Item 1b
(also multi-line)
...and extra-indented
1. Item 2
1. Item 2a
(multi-line)
1. Item 2b
(also multi-line)
...and extra-indented
1. Item 3
(multi-line but under-indented)
1. Item 4
* unordered item
(multi-line)
* unordered item
(multi-line and extra-indented)
...partially
* unordered item
(multi-line and under-indented)
12 changes: 12 additions & 0 deletions test/cases/list-item-deindenting.raw.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<ol><li>Item 1<ol><li>Item 1a
(multi-line)</li><li>Item 1b
(also multi-line)
...and extra-indented</li></ol></li><li>Item 2<ol><li>Item 2a
(multi-line)</li><li>Item 2b
(also multi-line)
...and extra-indented</li></ol></li><li>Item 3
(multi-line but under-indented)</li><li>Item 4<ul><li>unordered item
(multi-line)</li><li>unordered item
(multi-line and extra-indented)
...partially</li><li>unordered item
(multi-line and under-indented)</li></ul></li></ol>
1 change: 1 addition & 0 deletions test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ describe('Parser', function () {
{
name: 'ordered-list-item',
attrs: [],
contentsIndent: 3,
contents: [
{
contents: 'Foo. ',
Expand Down
2 changes: 1 addition & 1 deletion test/run-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('baselines', () => {
? ecmarkdown.fragment
: ecmarkdown.algorithm;
let rawOutput = processor(input);
let output = beautify(rawOutput);
let output = file.endsWith('.raw.ecmarkdown') ? rawOutput + '\n' : beautify(rawOutput);
let existing = fs.existsSync(snapshotFile) ? fs.readFileSync(snapshotFile, 'utf8') : null;
if (shouldUpdate) {
if (existing !== output) {
Expand Down