showdown-section-groups
Version:
A showdown extension that wraps headers and paragraphs in sections
71 lines (59 loc) • 1.78 kB
JavaScript
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(['exports', 'showdown'], factory);
} else if (typeof exports !== "undefined") {
factory(exports, require('showdown'));
} else {
var mod = {
exports: {}
};
factory(mod.exports, global.showdown);
global.showdownSectionGroups = mod.exports;
}
})(this, function (exports, _showdown) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = showdownSectionGroups;
var _showdown2 = _interopRequireDefault(_showdown);
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
function showdownSectionGroups() {
return {
type: 'output',
filter(html) {
let outputString = '';
let sectionOpen = false;
let inHeader = false;
let id = '';
for (let i = 0; i < html.length; i += 1) {
if (html.substring(i, i + 3).match(/<h\d/)) {
inHeader = true;
if (sectionOpen) {
outputString += '</section>\n';
}
}
if (inHeader && html.substring(i, i + 4).match(/id="/)) {
[, id] = html.substring(i, html.length).match(/^id="(.*)"/);
}
if (html.substring(i - 5, i).match(/<\/h\d>/)) {
outputString += `\n<section aria-labelledby="${id}">`;
id = '';
inHeader = false;
sectionOpen = true;
}
outputString += html[i];
}
if (sectionOpen) {
outputString += '\n</section>';
}
return outputString;
}
};
}
_showdown2.default.extension('section-groups', showdownSectionGroups);
});