// const data = `
// ---
// hello: world
// foo: bar
// ---
// hello: world
// ---
// # Some article title
// > "hello world"
// > "foo bar"
// `
const data = `
-----
hello: world
# Some article title
-----
> "hello world"
> "foo bar"
`
function getYamlSection(data) {
let state = 0
let lines = []
let yamlSectionDashesLength = 0
for (const line of data.trim().split("\n")) {
if (state === 0) {
if (line.length >= 3 && line === "-".repeat(line.length)) {
state = 1
yamlSectionDashesLength = line.length
} else if (line.trim() !== "") {
return ''
}
} else if (state === 1) {
if (line.length === yamlSectionDashesLength && line === "-".repeat(yamlSectionDashesLength)) {
return lines.join('\n')
} else {
lines.push(line)
}
}
}
return ''
}
console.log(getYamlSection(data))
To embed this program on your website, copy the following code and paste it into your website's HTML: