This repository has been archived by the owner on Sep 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔥 Add a new helper to extract meta from body nodes (title + headers l…
…ist)
- Loading branch information
Showing
3 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
packages/helpers-transform/src/__tests__/__snapshots__/extractMetaFromBodyNode.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
117
packages/helpers-transform/src/__tests__/extractMetaFromBodyNode.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } : {}) | ||
}; | ||
}; |