Skip to content

Commit 63d949f

Browse files
committed
feat(metadata): add support for embedded metadata
A simple metadata parser can be useful in markdown documents. This commit introduces the feature, with the following syntax: --- or ««« at tstart of the document, (optionally) followed by a alphanumeric format identifier followed by key value pairs separated by a colon and a space followed by --- or  Also, adds methods for retrieving the parsed metadata, namely: getMetadata() and getMetadataFormat Closes #260
1 parent a8427c9 commit 63d949f

31 files changed

+662
-41
lines changed

dist/showdown.js

Lines changed: 173 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/converter.js

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,17 @@ showdown.Converter = function (converterOptions) {
4343
/**
4444
* The flavor set in this converter
4545
*/
46-
setConvFlavor = setFlavor;
46+
setConvFlavor = setFlavor,
47+
48+
/**
49+
* Metadata of the document
50+
* @type {{parsed: {}, raw: string, format: string}}
51+
*/
52+
metadata = {
53+
parsed: {},
54+
raw: '',
55+
format: ''
56+
};
4757

4858
_constructor();
4959

@@ -255,7 +265,12 @@ showdown.Converter = function (converterOptions) {
255265
langExtensions: langExtensions,
256266
outputModifiers: outputModifiers,
257267
converter: this,
258-
ghCodeBlocks: []
268+
ghCodeBlocks: [],
269+
metadata: {
270+
parsed: {},
271+
raw: '',
272+
format: ''
273+
}
259274
};
260275

261276
// This lets us use ¨ trema as an escape char to avoid md5 hashes
@@ -299,6 +314,7 @@ showdown.Converter = function (converterOptions) {
299314
});
300315

301316
// run the sub parsers
317+
text = showdown.subParser('metadata')(text, options, globals);
302318
text = showdown.subParser('hashPreCodeTags')(text, options, globals);
303319
text = showdown.subParser('githubCodeBlocks')(text, options, globals);
304320
text = showdown.subParser('hashHTMLBlocks')(text, options, globals);
@@ -315,13 +331,15 @@ showdown.Converter = function (converterOptions) {
315331
text = text.replace(/¨T/g, '¨');
316332

317333
// render a complete html document instead of a partial if the option is enabled
318-
text = showdown.subParser('completeHTMLOutput')(text, options, globals);
334+
text = showdown.subParser('completeHTMLDocument')(text, options, globals);
319335

320336
// Run output modifiers
321337
showdown.helper.forEach(outputModifiers, function (ext) {
322338
text = showdown.subParser('runExtension')(ext, text, options, globals);
323339
});
324340

341+
// update metadata
342+
metadata = globals.metadata;
325343
return text;
326344
};
327345

@@ -429,4 +447,50 @@ showdown.Converter = function (converterOptions) {
429447
output: outputModifiers
430448
};
431449
};
450+
451+
/**
452+
* Get the metadata of the previously parsed document
453+
* @param raw
454+
* @returns {string|{}}
455+
*/
456+
this.getMetadata = function (raw) {
457+
if (raw) {
458+
return metadata.raw;
459+
} else {
460+
return metadata.parsed;
461+
}
462+
};
463+
464+
/**
465+
* Get the metadata format of the previously parsed document
466+
* @returns {string}
467+
*/
468+
this.getMetadataFormat = function () {
469+
return metadata.format;
470+
};
471+
472+
/**
473+
* Private: set a single key, value metadata pair
474+
* @param {string} key
475+
* @param {string} value
476+
*/
477+
this._setMetadataPair = function (key, value) {
478+
metadata.parsed[key] = value;
479+
};
480+
481+
/**
482+
* Private: set metadata format
483+
* @param {string} format
484+
*/
485+
this._setMetadataFormat = function (format) {
486+
metadata.format = format;
487+
};
488+
489+
/**
490+
* Private: set metadata raw text
491+
* @param {string} raw
492+
*/
493+
this._setMetadataRaw = function (raw) {
494+
metadata.raw = raw;
495+
};
432496
};

src/options.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,15 @@ function getDefaultOpts (simple) {
151151
description: 'Enable support for underline. Syntax is double or triple underscores: `__underline word__`. With this option enabled, underscores no longer parses into `<em>` and `<strong>`',
152152
type: 'boolean'
153153
},
154-
completeHTMLOutput: {
154+
completeHTMLDocument: {
155155
defaultValue: false,
156156
description: 'Outputs a complete html document, including `<html>`, `<head>` and `<body>` tags',
157157
type: 'boolean'
158+
},
159+
metadata: {
160+
defaultValue: false,
161+
description: 'Enable support for document metadata (defined at the top of the document between `«««` and `»»»` or between `---` and `---`).',
162+
type: 'boolean'
158163
}
159164
};
160165
if (simple === false) {

0 commit comments

Comments
 (0)