Skip to content

Commit

Permalink
feat(completeHTMLOutput): add option to output a complete HTML document
Browse files Browse the repository at this point in the history
  • Loading branch information
tivie committed Dec 10, 2017
1 parent cc19d9b commit a8427c9
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 6 deletions.
28 changes: 27 additions & 1 deletion dist/showdown.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/showdown.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/showdown.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/converter.js
Expand Up @@ -314,6 +314,9 @@ showdown.Converter = function (converterOptions) {
// attacklab: Restore tremas
text = text.replace(/¨T/g, '¨');

// render a complete html document instead of a partial if the option is enabled
text = showdown.subParser('completeHTMLOutput')(text, options, globals);

// Run output modifiers
showdown.helper.forEach(outputModifiers, function (ext) {
text = showdown.subParser('runExtension')(ext, text, options, globals);
Expand Down
5 changes: 5 additions & 0 deletions src/options.js
Expand Up @@ -150,6 +150,11 @@ function getDefaultOpts (simple) {
defaultValue: false,
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>`',
type: 'boolean'
},
completeHTMLOutput: {
defaultValue: false,
description: 'Outputs a complete html document, including `<html>`, `<head>` and `<body>` tags',
type: 'boolean'
}
};
if (simple === false) {
Expand Down
17 changes: 17 additions & 0 deletions src/subParsers/completeHTMLOutput.js
@@ -0,0 +1,17 @@
/**
* Turn Markdown link shortcuts into XHTML <a> tags.
*/
showdown.subParser('completeHTMLOutput', function (text, options, globals) {
'use strict';

if (!options.completeHTMLOutput) {
return text;
}

text = globals.converter._dispatch('completeHTMLOutput.before', text, options, globals);

text = '<html>\n<head>\n<meta charset="UTF-8">\n</head>\n<body>\n' + text.trim() + '\n</body>\n</html>';

text = globals.converter._dispatch('completeHTMLOutput.after', text, options, globals);
return text;
});
14 changes: 14 additions & 0 deletions test/features/completeHTMLOutput/simple.html
@@ -0,0 +1,14 @@
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<p>This is a <strong>markdown</strong> file</p>
<p>Converted into a full HTML document</p>
<ul>
<li>this</li>
<li>is</li>
<li>awesome</li>
</ul>
</body>
</html>
7 changes: 7 additions & 0 deletions test/features/completeHTMLOutput/simple.md
@@ -0,0 +1,7 @@
This is a **markdown** file

Converted into a full HTML document

- this
- is
- awesome
13 changes: 12 additions & 1 deletion test/node/testsuite.features.js
Expand Up @@ -14,7 +14,8 @@ var bootstrap = require('../bootstrap.js'),
emojisSuite = bootstrap.getTestSuite('test/features/emojis/'),
underlineSuite = bootstrap.getTestSuite('test/features/underline/'),
literalMidWordUnderscoresSuite = bootstrap.getTestSuite('test/features/literalMidWordUnderscores/'),
literalMidWordAsterisksSuite = bootstrap.getTestSuite('test/features/literalMidWordAsterisks/');
literalMidWordAsterisksSuite = bootstrap.getTestSuite('test/features/literalMidWordAsterisks/'),
completeHTMLOutputSuite = bootstrap.getTestSuite('test/features/completeHTMLOutput/');

describe('makeHtml() features testsuite', function () {
'use strict';
Expand Down Expand Up @@ -231,4 +232,14 @@ describe('makeHtml() features testsuite', function () {
it(suite[i].name.replace(/-/g, ' '), assertion(suite[i], converter));
}
});

/** test completeHTMLOutput option **/
describe('completeHTMLOutput option', function () {
var converter,
suite = completeHTMLOutputSuite;
for (var i = 0; i < suite.length; ++i) {
converter = new showdown.Converter({completeHTMLOutput: true});
it(suite[i].name.replace(/-/g, ' '), assertion(suite[i], converter));
}
});
});

0 comments on commit a8427c9

Please sign in to comment.