summaryrefslogtreecommitdiffstats
path: root/lib/page/html.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/page/html.js')
-rw-r--r--lib/page/html.js57
1 files changed, 57 insertions, 0 deletions
diff --git a/lib/page/html.js b/lib/page/html.js
new file mode 100644
index 0000000..f828d11
--- /dev/null
+++ b/lib/page/html.js
@@ -0,0 +1,57 @@
+var _ = require('lodash');
+var cheerio = require('cheerio');
+var domSerializer = require('dom-serializer');
+var slug = require('github-slugid');
+
+var Promise = require('../utils/promise');
+
+// Render a cheerio DOM as html
+function renderDOM($, dom, options) {
+ if (!dom && $._root && $._root.children) {
+ dom = $._root.children;
+ }
+ options = options|| dom.options || $._options;
+ return domSerializer(dom, options);
+}
+
+function HTMLPipeline(htmlString, opts) {
+ _.bindAll(this);
+
+ this.opts = _.defaults(opts || {}, {
+ convertImages: true
+ });
+
+ this.$ = cheerio.load(htmlString, {
+ // We should parse html without trying to normalize too much
+ xmlMode: false,
+
+ // SVG need some attributes to use uppercases
+ lowerCaseAttributeNames: false,
+ lowerCaseTags: false
+ });
+}
+
+// Add ID to headings
+HTMLPipeline.prototype.addHeadingIDs = function() {
+ var that = this;
+
+ this.$('h1,h2,h3,h4,h5,h6').each(function() {
+ // Already has an ID?
+ if (that.$(this).attr('id')) return;
+
+ that.$(this).attr('id', slug(that.$(this).text()));
+ });
+};
+
+// Write content to the pipeline
+HTMLPipeline.prototype.output = function() {
+ var that = this;
+
+ return Promise()
+ .then(this.addHeadingIDs)
+ .then(function() {
+ return renderDOM(that.$);
+ });
+};
+
+module.exports = HTMLPipeline;