summaryrefslogtreecommitdiffstats
path: root/lib/page/html.js
diff options
context:
space:
mode:
authorSamy Pesse <samypesse@gmail.com>2016-02-11 21:44:38 +0100
committerSamy Pesse <samypesse@gmail.com>2016-02-11 21:44:38 +0100
commit669f3b39849890c48171d807225cd6eaa3c9086b (patch)
treebc07fefc4e13ac8f737174166ac1d19512379298 /lib/page/html.js
parente7eed2abbe91fa44bd071819123bd9ea04d1702a (diff)
downloadgitbook-669f3b39849890c48171d807225cd6eaa3c9086b.zip
gitbook-669f3b39849890c48171d807225cd6eaa3c9086b.tar.gz
gitbook-669f3b39849890c48171d807225cd6eaa3c9086b.tar.bz2
Add base for normalizing html
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;