Skip to content

Commit

Permalink
feat: 🎸 add cnt field to all footnotes to track their order
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Feb 27, 2019
1 parent 9314a3a commit 61b0788
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/__tests__/mdastToFlat.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('structure', () => {
contents: [],
definitions: {},
footnotes: {},
footnoteOrder: [],
});
});

Expand Down Expand Up @@ -103,6 +104,7 @@ describe('structure', () => {
contents: [1, 3],
definitions: {},
footnotes: {},
footnoteOrder: [],
});
});

Expand Down Expand Up @@ -170,6 +172,7 @@ describe('structure', () => {
click: 4,
},
footnotes: {},
footnoteOrder: [],
});
});

Expand Down Expand Up @@ -229,6 +232,7 @@ describe('structure', () => {
},
{
type: 'footnoteDefinition',
cnt: 1,
children: [5],
},
{
Expand Down Expand Up @@ -373,7 +377,7 @@ describe('structure', () => {
{type: 'text'},
{type: 'footnoteReference'},
{type: 'text'},
{type: 'footnoteDefinition'},
{type: 'footnoteDefinition', cnt: 1},
{type: 'paragraph'},
{type: 'text'},
{type: 'heading'},
Expand All @@ -382,7 +386,7 @@ describe('structure', () => {
{type: 'paragraph'},
{type: 'text'},
{type: 'footnoteReference'},
{type: 'footnoteDefinition'},
{type: 'footnoteDefinition', cnt: 2},
{type: 'paragraph'},
{type: 'text'},
{type: 'heading'},
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/replace.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ merge here
children: [10],
identifier: 'foot',
idx: 9,
cnt: 1,
},
{type: 'paragraph', children: [11], idx: 10},
{type: 'text', value: 'This is footnote 1', idx: 11},
Expand Down Expand Up @@ -150,7 +151,7 @@ merge here
url: 'mailto:gg@bets.com',
idx: 19,
},
{type: 'footnoteDefinition', children: [21], identifier: 'note', idx: 20},
{type: 'footnoteDefinition', children: [21], identifier: 'note', idx: 20, cnt: 2},
{type: 'paragraph', children: [22], idx: 21},
{type: 'text', value: 'is dis…', idx: 22},
],
Expand Down
2 changes: 2 additions & 0 deletions src/mdastToFlat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const mdastToFlat: MdastToFlat = (mdast) => {
const definitions: FlatDefinitions = {};
const footnotes: FlatFootnotes = {};
const footnoteOrder: number[] = [];
let footnoteCounter = 0;
const doc = {
nodes,
contents,
Expand Down Expand Up @@ -39,6 +40,7 @@ export const mdastToFlat: MdastToFlat = (mdast) => {
definitions[node.identifier] = idx;
return -1;
case 'footnoteDefinition':
(node as any).cnt = ++footnoteCounter;
footnotes[node.identifier] = idx;
footnoteOrder.push(idx);
return -1;
Expand Down
8 changes: 7 additions & 1 deletion src/replace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,17 @@ export const replace = (into: Flat, at: number, what: Flat): Flat => {
Object.keys(what.definitions).forEach(
(identifier) => (merged.definitions[identifier] = what.definitions[identifier] + mergeIdx),
);

// MERGE FOOTNOTES.
Object.keys(what.footnotes).forEach(
(identifier) => (merged.footnotes[identifier] = what.footnotes[identifier] + mergeIdx),
);
let footnoteCount = into.footnoteOrder.length;
for (const footnoteIndex of what.footnoteOrder) {
merged.footnoteOrder.push(footnoteIndex + mergeIdx);
const index = footnoteIndex + mergeIdx
footnoteCount++;
merged.footnoteOrder.push(index);
(merged.nodes[index] as any).cnt = footnoteCount;
}

return merged;
Expand Down

0 comments on commit 61b0788

Please sign in to comment.