Skip to content

Commit

Permalink
feat: flatten generated table (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatiusmb committed Jan 9, 2024
1 parent f95e37c commit 3c2bb2e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 38 deletions.
17 changes: 3 additions & 14 deletions workspace/marqua/src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,17 @@ export function parse(source) {
get table() {
/** @type {import('../types.js').MarquaTable[]} */
const table = [];
let parent = table; // reference to push
for (const line of stuffed.split('\n')) {
const match = line.trim().match(/^(#{2,4}) (.+)/);
if (!match) continue;

const [, h, title] = match;
const [delimited] = title.match(/\$\(.*\)/) || [''];

if (h.length === 2 || !table.length) {
parent = table;
} else {
parent = table[table.length - 1].sections;
if (h.length === 4) {
parent = parent[parent.length - 1].sections;
}
}

parent.push({
table.push({
id: generate.id(delimited.slice(2, -1) || title),
level: h.length,
title: title.replace(delimited, delimited.slice(2, -1)),
sections: [],
level: h.length,
});
}

Expand All @@ -63,7 +52,7 @@ export function parse(source) {
/**
* @param {string} raw
* @param {Record<string, any>} [memo]
* @returns {import('../types.js').FrontMatter [string]}
* @returns {import('../types.js').FrontMatter[string]}
*/
export function construct(raw, memo = {}) {
if (!/[:\-\[\]|#]/gm.test(raw)) return coerce(raw.trim());
Expand Down
38 changes: 15 additions & 23 deletions workspace/marqua/src/core/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ story and plot contents
id: 'story-plot',
level: 2,
title: '8/10 | story & plot',
sections: [],
},
]);
});
Expand Down Expand Up @@ -329,33 +328,26 @@ something here
id: 'simple-heading',
level: 2,
title: 'simple heading',
sections: [],
},
{
id: 'story-plot',
level: 2,
title: 'story & plot',
sections: [
{
id: 'subsection-of-story-and-plot',
level: 3,
title: 'subsection of story and plot',
sections: [
{
id: 'smallest-heading',
level: 4,
title: 'smallest heading',
sections: [],
},
],
},
{
id: 'second-subsection',
level: 3,
title: 'second subsection',
sections: [],
},
],
},
{
id: 'subsection-of-story-and-plot',
level: 3,
title: 'subsection of story and plot',
},
{
id: 'smallest-heading',
level: 4,
title: 'smallest heading',
},
{
id: 'second-subsection',
level: 3,
title: 'second subsection',
},
]);
});
Expand Down
1 change: 0 additions & 1 deletion workspace/marqua/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,4 @@ export interface MarquaTable {
id: string;
level: number;
title: string;
sections: MarquaTable[];
}

1 comment on commit 3c2bb2e

@vercel
Copy link

@vercel vercel bot commented on 3c2bb2e Jan 9, 2024

Choose a reason for hiding this comment

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

Please sign in to comment.