diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..9142239 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +# editorconfig.org +root = true + +[*] +indent_size = 2 +indent_style = space +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3adea5b --- /dev/null +++ b/.gitignore @@ -0,0 +1,54 @@ +###Node### + +# Logs +logs +*.log + +# Runtime data +pids +*.pid +*.seed + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directory +# Commenting this out is preferred by some people, see +# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- +node_modules + +# Users Environment Variables +.lock-wscript + + +###OSX### + +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear on external disk +.Spotlight-V100 +.Trashes + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk \ No newline at end of file diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..85e7c0e --- /dev/null +++ b/.npmignore @@ -0,0 +1,62 @@ +###Node### + +# Logs +logs +*.log + +# Runtime data +pids +*.pid +*.seed + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directory +# Commenting this out is preferred by some people, see +# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- +node_modules + +# Users Environment Variables +.lock-wscript + + +###OSX### + +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear on external disk +.Spotlight-V100 +.Trashes + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# npm specific + +test +.cz.json +.travis.yml +.editorconfig diff --git a/examples/index.js b/examples/index.js new file mode 100644 index 0000000..fde44b5 --- /dev/null +++ b/examples/index.js @@ -0,0 +1,36 @@ +'use strict'; + +/** + * html-to-markdown + * Copyright(c) 2015-2015 Harminder Virk + * MIT Licensed +*/ + +var converter = require('../index'); + +var html = `
This is a paragrap buddy
++ var name = "foo"; ++
+ This is super quote + In multiple lines ++ This is bold tag + Some italic +
([\s\S]*?)<\/p>/gim +var ulRegex = /
([\s\S]*?)<\/pre>/gim +var blockQuoteRegex = /([\s\S]*?)<\/blockquote>/gim +var boldRegex = /<(?:b|strong)>([\s\S]*?)<\/\w*>/gim +var italicRegex = /<(?:i|em)>([\s\S]*?)<\/\w*>/gim + +/** + * @description executes a regex to replace matched text with + * selected group with optional pre and postfix + * @method makeRegex + * @param {String} regex [description] + * @param {String} doc [description] + * @param {String} before [description] + * @param {String} after [description] + * @return {String} [description] + */ +function makeRegex (regex, doc, before, after, replaceFn) { + var matches = []; + var newDoc = doc; + var replaceString; + while(matches = regex.exec(doc)){ + if(matches && matches[1]){ + replaceString = before || ''; + var replaceText = matches[1].trim() + if(replaceFn && typeof(replaceFn) === 'function'){ + replaceText = replaceFn(matches) + } + replaceString += replaceText; + replaceString += after || ''; + newDoc = newDoc.replace(matches[0],replaceString); + } + } + return newDoc +} + +/** + * @description replaces html headings with equalent markdown + * syntax + * @method replaceHeading + * @param {String} doc [description] + * @return {String} [description] + */ +function replaceHeading (doc) { + return makeRegex(headingRegex, doc, null, null, function (match) { + return addHashes(match[1]) + match[2] + }); +} + +/** + * @description replaces ul section with equalent markdown + * syntax + * @method replaceUl + * @param {String} doc [description] + * @return {String} [description] + */ +function replaceUl (doc) { + return makeRegex(ulRegex, doc, null, null, function (match) { + return replaceLi(match[1],'ul') + }); +} + +/** + * @description replaces ol section with equalent markdown + * syntax + * @method replaceOl + * @param {String} doc [description] + * @return {String} [description] + */ +function replaceOl (doc) { + return makeRegex(olRegex, doc, null, null, function (match) { + return replaceLi(match[1],'ol') + }); +} + +/** + * @description replaces paragraph section with equalent markdown + * syntax + * @method replaceParagraph + * @param {String} doc [description] + * @return {String} [description] + */ +function replaceParagraph (doc) { + return makeRegex(pRegex, doc); +} + +/** + * @description replaces pre section with equalent markdown + * syntax + * @method replacePre + * @param {String} doc [description] + * @return {String} [description] + */ +function replacePre (doc) { + return makeRegex(preRegex, doc, '`', '`'); +} + +/** + * @description replaces blockquote section with equalent markdown + * syntax + * @method replaceBlockQuote + * @param {String} doc [description] + * @return {String} [description] + */ +function replaceBlockQuote (doc) { + return makeRegex(blockQuoteRegex, doc, '> '); +} + +/** + * @description replaces bold|strong section with equalent markdown + * syntax + * @method replaceBold + * @param {String} doc [description] + * @return {String} [description] + */ +function replaceBold (doc) { + return makeRegex(boldRegex, doc, '** ', ' **'); +} + +/** + * @description replaces i|em section with equalent markdown + * syntax + * @method replaceItalic + * @param {String} doc [description] + * @return {String} [description] + */ +function replaceItalic (doc) { + return makeRegex(italicRegex, doc, '* ', ' *'); +} + +/** + * @description replaces li tags with equalent markup based + * upon their parent tag + * @method replaceLi + * @param {String} doc [description] + * @param {String} tag [description] + * @return {String} [description] + */ +function replaceLi (doc, tag) { + var matches = []; + var newDoc = doc; + var replaceIndex = 0; + var replaceTag = ''; + while(matches = liRegex.exec(doc)) { + if (matches && matches[1]) { + if (tag !== 'ul') { + replaceIndex++; + replaceTag = replaceIndex + '. '; + }else{ + replaceTag = '* '; + } + newDoc = newDoc.replace(matches[0],replaceTag+matches[1].trim()); + } + } + return newDoc +} + +/** + * @descriptions adds number of hashes to headings + * based upon heading weight + * @method addHashes + * @param {Number} count [description] + */ +function addHashes (count) { + count = Number(count); + var string = ''; + for(var x=0;x/gim + +/** + * @description replacing unncessary html tags + * @method replaceExtras + * @param {String} doc + * @return {String} + */ +function replaceExtras(doc) { + var matches = []; + var newDoc = doc; + newDoc = newDoc.replace(extrasRegex,''); + return newDoc +} + +module.exports = { + + /** + * @description converts given html to a markdown + * document + * @method convert + * @param {String} html + * @return {String} + */ + convert: function (html) { + + /** + * replacing unncessary html tags + * @type {String} + */ + html = replaceExtras(html); + + /** + * looping through registered formatters + */ + for(var x=0;x