Merge branch 'mermaid-front-matter-support' of https://github.com/takac/hugo-theme-relearn into takac-mermaid-front-matter-support

This commit is contained in:
Sören Weber 2023-06-09 20:36:28 +02:00
commit 59428415f2
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D

View file

@ -171,9 +171,22 @@ function initMermaid( update, attrs ) {
}; };
var serializeGraph = function( graph ){ var serializeGraph = function( graph ){
// See https://github.com/mermaid-js/mermaid/blob/9a080bb975b03b2b1d4ef6b7927d09e6b6b62760/packages/mermaid/src/diagram-api/frontmatter.ts#L10
// for reference on the regex originally taken from jekyll
const frontMatterRegex = /^-{3}\s*[\n\r](.*?)[\n\r]-{3}\s*[\n\r]+/s;
const matches = graph.content.match(frontMatterRegex);
// if we find front matter, we need to put this before the `%%{init` directive.
if (matches) {
const ymlFrontMatter = graph.content.substring(0, matches[0].length);
const graphContent = graph.content.slice(matches[0].length);
var s = ymlFrontMatter + '\n%%{init: ' + JSON.stringify( graph.dir ) + '}%%\n';
s += graphContent;
return s;
} else {
var s = '%%{init: ' + JSON.stringify( graph.dir ) + '}%%\n'; var s = '%%{init: ' + JSON.stringify( graph.dir ) + '}%%\n';
s += graph.content; s += graph.content;
return s; return s;
}
}; };
var init_func = function( attrs ){ var init_func = function( attrs ){