You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
core/src/parsers/markdown.js

56 lines
1.4 KiB

const yaml = require('js-yaml')
const { marked } = require('marked')
const configStore = require('./../config.js')
const renderer = require('./../marked/renderer.js')
/**
* parse string of file, parse yaml and parse markdown
*
* @author Björn Hase <me@herr-hase.wtf>
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://gitea.node001.net/HerrHase/siteomat-webpack-plugin.git
*
*/
function parseMarkdownFile(fileString, dirPath = '') {
// regex get yaml section and markdown
// thanks to, https://github.com/getgrav/grav
const regex = new RegExp(/^(---\n(.+?)\n---){0,}(.*)$/gs)
const matches = regex.exec(fileString)
const result = {
fields: undefined,
content: ''
}
// check if yaml section not exists throw error
if (matches?.[2]) {
try {
result.fields = yaml.load(matches[2])
} catch (error) {
throw new Error('Yaml has errors!')
}
}
// if markdown section exits parse it to html 6565
if (matches?.[3]) {
// reset configStore
configStore.set('markedDirPath', false)
// check for dirPath and set it to configStore for marked/renderer.js
if (dirPath || dirPath === '') {
configStore.set('markedDirPath', dirPath)
}
marked.use({ renderer })
result.content = marked.parse(matches[3])
}
return result
}
module.exports = parseMarkdownFile