showdown/src/subParsers/makehtml/metadata.js

55 lines
1.5 KiB
JavaScript

/**
* Parse metadata at the top of the document
*/
showdown.subParser('makehtml.metadata', function (text, options, globals) {
'use strict';
if (!options.metadata) {
return text;
}
text = globals.converter._dispatch('makehtml.metadata.before', text, options, globals).getText();
function parseMetadataContents (content) {
// raw is raw so it's not changed in any way
globals.metadata.raw = content;
// escape chars forbidden in html attributes
// double quotes
content = content
// ampersand first
.replace(/&/g, '&')
// double quotes
.replace(/"/g, '"');
// Restore dollar signs and tremas
content = content
.replace(/¨D/g, '$$')
.replace(/¨T/g, '¨');
content = content.replace(/\n {4}/g, ' ');
content.replace(/^([\S ]+): +([\s\S]+?)$/gm, function (wm, key, value) {
globals.metadata.parsed[key] = value;
return '';
});
}
text = text.replace(/^\s*«««+\s*(\S*?)\n([\s\S]+?)\n»»»+\s*\n/, function (wholematch, format, content) {
parseMetadataContents(content);
return '¨M';
});
text = text.replace(/^\s*---+\s*(\S*?)\n([\s\S]+?)\n---+\s*\n/, function (wholematch, format, content) {
if (format) {
globals.metadata.format = format;
}
parseMetadataContents(content);
return '¨M';
});
text = text.replace(/¨M/g, '');
text = globals.converter._dispatch('makehtml.metadata.after', text, options, globals).getText();
return text;
});