Browse Source

feat(completeHTMLOutput): add option to output a complete HTML document

Estevao Soares dos Santos 7 years ago
parent
commit
a8427c9423

+ 27 - 1
dist/showdown.js

@@ -1,4 +1,4 @@
-;/*! showdown v 1.8.4 - 09-12-2017 */
+;/*! showdown v 1.8.4 - 10-12-2017 */
 (function(){
 /**
  * Created by Tivie on 13-07-2015.
@@ -152,6 +152,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) {
@@ -2438,6 +2443,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);
@@ -2888,6 +2896,24 @@ showdown.subParser('codeSpans', function (text, options, globals) {
   return text;
 });
 
+/**
+ * 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;
+});
+
 /**
  * Convert all tabs to spaces
  */

File diff suppressed because it is too large
+ 0 - 0
dist/showdown.js.map


File diff suppressed because it is too large
+ 0 - 1
dist/showdown.min.js


File diff suppressed because it is too large
+ 0 - 0
dist/showdown.min.js.map


+ 3 - 0
src/converter.js

@@ -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);

+ 5 - 0
src/options.js

@@ -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) {

+ 17 - 0
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 - 0
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 - 0
test/features/completeHTMLOutput/simple.md

@@ -0,0 +1,7 @@
+This is a **markdown** file
+
+Converted into a full HTML document
+
+ - this
+ - is
+ - awesome

+ 12 - 1
test/node/testsuite.features.js

@@ -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';
@@ -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));
+    }
+  });
 });

Some files were not shown because too many files changed in this diff