Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Commit

Permalink
🔥 Add a new helper to extract meta from body nodes (title + headers l…
Browse files Browse the repository at this point in the history
…ist)
  • Loading branch information
MoOx committed May 2, 2018
1 parent efb2e8f commit da0d4da
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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",
],
],
"title": "Auto title",
}
`;
117 changes: 117 additions & 0 deletions packages/helpers-transform/src/__tests__/extractMetaFromBodyNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// @flow

import extractMetaFromBodyNode from "../extractMetaFromBodyNode.js";

it("should guess partial data from body AST", () => {
expect(
extractMetaFromBodyNode({
c: [
{
c: [
{
c: [
{
p: {
className: "icon icon-link"
},
t: "span"
}
],
p: {
"aria-hidden": true,
href: "#auto-title"
},
t: "a"
},
"Auto title"
],
p: {
id: "auto-title"
},
t: "h1"
},
"\n",
{
c: [
{
c: [
{
p: {
className: "icon icon-link"
},
t: "span"
}
],
p: {
"aria-hidden": true,
href: "#sub-title"
},
t: "a"
},
"Sub title"
],
p: {
id: "sub-title"
},
t: "h2"
},
"\n",
{
c: ["Content"],
t: "p"
},
"\n",
{
c: [
{
c: [
{
p: {
className: "icon icon-link"
},
t: "span"
}
],
p: {
"aria-hidden": true,
href: "#another-level-2"
},
t: "a"
},
"Another Level 2"
],
p: {
id: "another-level-2"
},
t: "h2"
},
"\n",
{
c: [
{
c: [
{
p: {
className: "icon icon-link"
},
t: "span"
}
],
p: {
"aria-hidden": true,
href: "#a-level-3"
},
t: "a"
},
"A level 3"
],
p: {
id: "a-level-3"
},
t: "h3"
}
],
t: "div"
})
).toMatchSnapshot();
});
43 changes: 43 additions & 0 deletions packages/helpers-transform/src/extractMetaFromBodyNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// @flow

type Node =
| string
| {|
t?: string,
p?: Object,
c?: Node | $ReadOnlyArray<Node>
|};

const renderText = (node?: Node) => {
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) => {
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 (Array.isArray(node.c)
? // $FlowFixMe stfu
node.c.reduce((acc, child: Node) => acc.concat(getHeaders(child)), [])
: // $FlowFixMe stfu
getHeaders(node.c)
).filter(h => h);
};

export default (node: Node) => {
const headers = getHeaders(node);
const firstH1 = headers.find(h => h[0] === 1);
return {
...(firstH1 ? { title: firstH1[1] } : {}),
...(headers.length > 0 ? { headers } : {})
};
};

0 comments on commit da0d4da

Please sign in to comment.