Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create alternative syntax for frontmatter #2145

Merged
merged 9 commits into from
Apr 1, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/core/src/lib/markdown-it/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ markdownIt.use(require('markdown-it-mark'))
.use(require('./plugins/markdown-it-icons'))
.use(require('./plugins/markdown-it-footnotes'))
.use(require('./plugins/markdown-it-center-text'))
.use(require('./plugins/markdown-it-colour-text'));
.use(require('./plugins/markdown-it-colour-text'))
.use(require('./plugins/markdown-it-alt-frontmatter'));

// fix table style
markdownIt.renderer.rules.table_open = _.constant(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

// Process "---" => "<frontmatter>

module.exports = function alt_frontmatter_plugin(md) {
function alt_frontmatter(state, startLine, endLine, silent) {
const fmSymbol = "---";
const keyValueRegex = new RegExp("^\\w+:\\s+.*");

let lineStart = state.bMarks[startLine] + state.tShift[startLine];
let lineEnd = state.eMarks[startLine];
let marker = state.src.slice(lineStart, lineEnd);
if (marker !== fmSymbol) { return false; }

let haveEndMarker = false;
let nextLine = startLine + 1;
for (; nextLine < endLine; nextLine++) {
lineStart = state.bMarks[nextLine] + state.tShift[nextLine];
lineEnd = state.eMarks[nextLine];

let currLine = state.src.slice(lineStart, lineEnd).trim();
if (currLine === fmSymbol) {
haveEndMarker = true;
break;
}
if (!(keyValueRegex.test(currLine) || currLine.length === 0)) {
// terminates if not key value or blank line.
return false;
}
}

if (!haveEndMarker) { return false; }
// If a fence has heading spaces, they should be removed from its inner block
let len = state.sCount[startLine];
state.line = nextLine + (haveEndMarker ? 1 : 0);

let token = state.push('alt_frontmatter', '', 0);
token.info = fmSymbol;
token.content = state.getLines(startLine + 1, nextLine, len, true);
token.markup = marker;
token.map = [ startLine, state.line ];
return true;
}

md.block.ruler.before('fence', 'alt_frontmatter', alt_frontmatter);
md.renderer.rules.alt_frontmatter = (tokens, idx, options, env, slf) => {
const token = tokens[idx];
if (token.type === 'alt_frontmatter' && token.info === '---') {
return '<frontmatter>\n' + token.content + '</frontmatter>\n';
}
return slf.renderToken(tokens, idx, options, env, slf);
};
}