vnote/src/utils/markdown-it/markdown-it-front-matter.js

121 lines
4.1 KiB
JavaScript

(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.markdownitFrontMatter = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
// Process front matter and pass to cb
//
'use strict';
module.exports = function front_matter_plugin(md, cb) {
var min_markers = 3,
marker_str = '-',
marker_char = marker_str.charCodeAt(0),
marker_len = marker_str.length
function frontMatter(state, startLine, endLine, silent) {
var pos, nextLine, marker_count, markup, token,
old_parent, old_line_max, start_content,
auto_closed = false,
start = state.bMarks[startLine] + state.tShift[startLine],
max = state.eMarks[startLine];
// Check out the first character of the first line quickly,
// this should filter out non-front matter
//
if (startLine !== 0 || marker_char !== state.src.charCodeAt(0)) { return false; }
// Check out the rest of the marker string
//
for (pos = start + 1; pos <= max; pos++) { // while pos <= 3
if (marker_str[(pos - start) % marker_len] !== state.src[pos]) {
start_content = pos + 1
break;
}
}
marker_count = Math.floor((pos - start) / marker_len);
if (marker_count < min_markers) { return false; }
pos -= (pos - start) % marker_len;
// Since start is found, we can report success here in validation mode
//
if (silent) { return true; }
// Search for the end of the block
//
nextLine = startLine;
for (;;) {
nextLine++;
if (nextLine >= endLine) {
// unclosed block should be autoclosed by end of document.
// also block seems to be autoclosed by end of parent
break;
}
start = state.bMarks[nextLine] + state.tShift[nextLine];
max = state.eMarks[nextLine];
if (start < max && state.sCount[nextLine] < state.blkIndent) {
// non-empty line with negative indent should stop the list:
// - ```
// test
break;
}
if (marker_char !== state.src.charCodeAt(start)) { continue; }
if (state.sCount[nextLine] - state.blkIndent >= 4) {
// closing fence should be indented less than 4 spaces
continue;
}
for (pos = start + 1; pos <= max; pos++) {
if (marker_str[(pos - start) % marker_len] !== state.src[pos]) {
break;
}
}
// closing code fence must be at least as long as the opening one
if (Math.floor((pos - start) / marker_len) < marker_count) { continue; }
// make sure tail has spaces only
pos -= (pos - start) % marker_len;
pos = state.skipSpaces(pos);
if (pos < max) { continue; }
// found!
auto_closed = true;
break;
}
old_parent = state.parentType;
old_line_max = state.lineMax;
state.parentType = 'container';
// this will prevent lazy continuations from ever going past our end marker
state.lineMax = nextLine;
token = state.push('front_matter', null, 0);
token.hidden = true;
token.markup = state.src.slice(startLine, pos)
token.block = true;
token.map = [ startLine, pos ];
state.parentType = old_parent;
state.lineMax = old_line_max;
state.line = nextLine + (auto_closed ? 1 : 0);
cb(state.src.slice(start_content, start - 1))
return true;
}
md.block.ruler.before('table', 'front_matter', frontMatter, {
alt: [ 'paragraph', 'reference', 'blockquote', 'list' ]
});
};
},{}]},{},[1])(1)
});