/
meta.ts
74 lines (63 loc) · 1.36 KB
/
meta.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import defu from 'defu'
import { MarkdownRoot } from '../types'
import { flatUnwrap, nodeTextContent } from './utils/node'
export function processHeading (body: MarkdownRoot) {
let title = ''
let description = ''
const children = body.children
// top level `text` can be ignored
.filter(node => node.type !== 'text')
if (children.length && children[0].tag === 'h1') {
/**
* Remove node
*/
const node = children.shift()!
/**
* Remove anchor link from H1 tag
*/
node.children = flatUnwrap(node.children, ['a'])
/**
* Generate title
*/
title = nodeTextContent(node)
/**
* Inject class
*/
node.props = defu(node.props || {}, {
class: 'd-heading-title'
})
}
if (children.length && children[0].tag === 'p') {
/**
* Remove node
*/
const node = children.shift()!
/**
* Generate description
*/
description = nodeTextContent(node)
/**
* Inject class
*/
node.props = defu(node.props || {}, {
class: 'd-heading-description'
})
}
if (children.length && children[0].tag === 'hr') {
/**
* Remove node
*/
const node = children.shift()!
/**
* Inject class
*/
node.props = defu(node.props || {}, {
class: 'd-heading-hr'
})
}
return {
title,
description,
body
}
}