Skip to content

Commit

Permalink
Preserve list marker (markdoc#348)
Browse files Browse the repository at this point in the history
* Preserves list marker in parser and formatter

* Updates the list schema to add the marker attribute

* Prettier

* Minor tweaks
  • Loading branch information
rpaul-stripe authored Mar 28, 2023
1 parent 1156991 commit d46832b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
43 changes: 39 additions & 4 deletions src/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,41 @@ Yes!
check(source, expected);
stable(expected);
});

it('preserving list marker', () => {
const source = `
- foo
- bar
* baz
* qux
1) foo
2) bar
3) baz
1. foo
2. bar
3. baz
`;
const expected = `
- foo
- bar
* baz
* qux
1) foo
1) bar
1) baz
1. foo
1. bar
1. baz
`;
check(source, expected);
stable(expected);
});

it('"loose" lists', () => {
const source = `
- One
Expand Down Expand Up @@ -558,7 +593,7 @@ Yes!
Markdoc uses…`;

const expected = `
- Create your CNAME record
* Create your CNAME record
1. Click **Add record**.
Expand Down Expand Up @@ -632,9 +667,9 @@ Yes!
`;

const expected = `
- **One**{% colspan=1 %}
- **Two**{% colspan=2 %}
- **Three**{% colspan=3 %}
* **One**{% colspan=1 %}
* **Two**{% colspan=2 %}
* **Three**{% colspan=3 %}
`;

check(source, expected);
Expand Down
14 changes: 8 additions & 6 deletions src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ type Options = {
const SPACE = ' ';
const SEP = ', '; // Value separator
const NL = '\n'; // Newline
const OL = '1. '; // Ordered list
const UL = '- '; // Unordered list
const OL = '.'; // Ordered list
const UL = '-'; // Unordered list

const MAX_TAG_OPENING_WIDTH = 80;

Expand Down Expand Up @@ -287,10 +287,12 @@ function* formatNode(n: Node, o: Options = {}) {
break;
}
case 'list': {
const prefix = n.attributes.ordered ? OL : UL;
const prefix = n.attributes.ordered
? `1${n.attributes.marker ?? OL}`
: n.attributes.marker ?? UL;
for (const child of n.children) {
const d = format(child, increment(no, prefix.length)).trim();
yield NL + indent + prefix + d;
const d = format(child, increment(no, prefix.length + 1)).trim();
yield NL + indent + prefix + ' ' + d;
}
yield NL;
break;
Expand Down Expand Up @@ -353,7 +355,7 @@ function* formatNode(n: Node, o: Options = {}) {
yield indent + '---';
}
for (const d of row) {
yield NL + indent + UL + d;
yield NL + indent + UL + ' ' + d;
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ function handleAttrs(token: Token, type: string) {
case 'heading':
return { level: Number(token.tag.replace('h', '')) };
case 'list':
return { ordered: token.type.startsWith('ordered') };
return {
ordered: token.type.startsWith('ordered'),
marker: token.markup,
};
case 'link': {
const attrs = Object.fromEntries(token.attrs);
return attrs.title
Expand Down
1 change: 1 addition & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export const list: Schema = {
children: ['item'],
attributes: {
ordered: { type: Boolean, render: false, required: true },
marker: { type: String, render: false },
},
transform(node, config) {
return new Tag(
Expand Down

0 comments on commit d46832b

Please sign in to comment.