Permalink
Browse files

`@phenomic/helpers-transform`: extractMetaFromBodyNode: replace `he…

…aders` by `headings`

 `@phenomic/plugin-transform-markdown`: replace `headers` by `headings`
 `@phenomic/plugin-transform-asciidoc`: replace `headers` by `headings`
  • Loading branch information...
MoOx committed May 11, 2018
1 parent 8d7ef84 commit 1b68eff0c6a0e299da3e3c7a9c897cc4338525aa
@@ -2,24 +2,28 @@
exports[`should guess partial data from body AST 1`] = `
Object {
"headers": Array [
Array [
1,
"Auto title",
],
Array [
2,
"Sub title",
],
Array [
2,
"Another Level 2",
],
Array [
3,
"A level 3",
],
"headings": Array [
Object {
"id": "auto-title",
"level": 1,
"text": "Auto title",
},
Object {
"id": "sub-title",
"level": 2,
"text": "Sub title",
},
Object {
"id": "another-level-2",
"level": 2,
"text": "Another Level 2",
},
Object {
"id": "a-level-3",
"level": 3,
"text": "A level 3",
},
],
"title": "Auto title",
"title": undefined,
}
`;
@@ -8,36 +8,44 @@ type Node =
c?: Node | $ReadOnlyArray<Node>
|};
const renderText = (node?: Node) => {
const renderText = (node?: Node): string => {
if (!node) return "";
if (typeof node === "string") return node;
return Array.isArray(node.c)
? node.c.map((child: Node) => renderText(child)).join("")
: renderText(node.c);
};
const getHeaders = (node?: Node) => {
const getHeadings = (
node?: Node
): $ReadOnlyArray<{ level: number, text: string, id: string }> => {
if (!node) return [];
if (typeof node.t === "string") {
const tag = node.t;
const level = parseInt(tag[1], 10);
if (tag[0] === "h" && !isNaN(level)) {
return [[level, renderText(node)]];
return [
{
level,
text: renderText(node),
id: node.p && node.p.id ? String(node.p.id) : ""
}
];
}
}
return (Array.isArray(node.c)
? // $FlowFixMe stfu
node.c.reduce((acc, child: Node) => acc.concat(getHeaders(child)), [])
node.c.reduce((acc, child: Node) => acc.concat(getHeadings(child)), [])
: // $FlowFixMe stfu
getHeaders(node.c)
getHeadings(node.c)
).filter(h => h);
};
export default (node: Node) => {
const headers = getHeaders(node);
const firstH1 = headers.find(h => h[0] === 1);
const headings = getHeadings(node);
const firstH1 = headings.find(h => h.level === 1);
return {
...(firstH1 ? { title: firstH1[1] } : {}),
...(headers.length > 0 ? { headers } : {})
...(headings.length > 0 ? { headings } : {})
};
};
@@ -425,11 +425,12 @@ Object {
"filetype-html": "",
"firstname": "Ezra",
"header_footer": "false",
"headers": Array [
Array [
2,
"Heading",
],
"headings": Array [
Object {
"id": "_heading",
"level": 2,
"text": "Heading",
},
],
"iconfont-remote": "",
"iconsdir": "/path/to/icons",
@@ -486,11 +487,12 @@ Object {
"filetype-html": "",
"firstname": "Ezra",
"header_footer": "false",
"headers": Array [
Array [
2,
"Heading",
],
"headings": Array [
Object {
"id": "_heading",
"level": 2,
"text": "Heading",
},
],
"iconfont-remote": "",
"iconsdir": "/path/to/icons",
@@ -192,22 +192,24 @@ Object {
],
"t": "div",
},
"headers": Array [
Array [
2,
"Test",
],
"headings": Array [
Object {
"id": "test",
"level": 2,
"text": "Test",
},
],
"test": "a",
"test2": "b",
"title": "file.json",
},
"partial": Object {
"headers": Array [
Array [
2,
"Test",
],
"headings": Array [
Object {
"id": "test",
"level": 2,
"text": "Test",
},
],
"test": "a",
"test2": "b",

0 comments on commit 1b68eff

Please sign in to comment.