0π
β
The parse result of the content is stored in article.body.children[]
. Each child contains the following node data:
tag
β HTML tag (e.g.,h2
,h3
)type
β Element type (e.g.,element
,text
)value
β Element value (the text contents)props
β Extra props data, includingid
children[]
β Child nodes
You could use that info to parse the nodes into a convenient data structure that stores release info, such as:
title
from thetext
child ofh2
id
fromprops
ofh2
changes[]
to hold the change lines, each containing:id
fromprops
ofh4
type
from thetext
child ofh3
text
from thetext
child ofh4
const elems = article.body.children.filter(node => node.type === 'element')
let rel = {}
let type = ''
for (const node of elems) {
if (node.tag === 'h2') {
rel = {
id: node.props.id,
title: node.children.find(c => c.type === 'text').value,
changes: []
}
this.releases.push(rel)
} else if (node.tag === 'h3') {
type = node.children.find(c => c.type === 'text').value
} else if (node.tag === 'h4') {
rel.changes.push({
id: node.props.id,
type,
text: node.children.find(c => c.type === 'text').value,
})
}
}
Then, use v-for
to render the list of release info parsed above.
<article v-for="rel of releases" :key="rel.id">
<h3 class="title">{{ rel.title }}</h3>
<section class="change" v-for="change in rel.changes" :key="change.id">
<div class="type" :class="change.type">{{ change.type }}</div>
<div class="text">{{ change.text }}</div>
</section>
</article>
Source:stackexchange.com