diff --git a/bin/illiterate b/bin/illiterate index aae5a11..a0bea32 100755 --- a/bin/illiterate +++ b/bin/illiterate @@ -1,12 +1,12 @@ #!/usr/bin/env node var path = require('path'), - fs = require('fs'), - lib = path.join(path.dirname(fs.realpathSync(__filename)), '../lib'), - illiterate = require(lib + '/illiterate.js'); + fs = require('fs'), + lib = path.join(path.dirname(fs.realpathSync(__filename)), '../lib'), + illiterate = require(lib + '/illiterate.js') -process.argv.slice(2).forEach(function(filename){ - var file_contents = fs.readFileSync(filename).toString('UTF-8'); +process.argv.slice(2).forEach(function (filename) { + var file_contents = fs.readFileSync(filename).toString('UTF-8') // TODO: specify output file to return, or folder to output all files into - console.log(illiterate(file_contents).default); -}); + console.log(illiterate(file_contents)) +}) diff --git a/lib/illiterate.js b/lib/illiterate.js index 89c00e8..ebca30f 100644 --- a/lib/illiterate.js +++ b/lib/illiterate.js @@ -1,34 +1,17 @@ !function(){ var root = this, illiterate; - var _ = require('lodash'), - marked = require('marked'); - illiterate = function(text){ - var out = { default: [] }, - target = 'default', - // from marked.js Inline-Level Grammar... - srcPattern = /^\[((?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*)\]\s*\[src:\/\/([^\]]*)\]/, - m, - lexed = marked.lexer(text, {}); - _.each(lexed, function(item){ - if(!!item.text && item.text[0] === '[' && (m = item.text.match(srcPattern))) { - target = m[2]; - out[target] = out[target] || []; + var _ = require('lodash'), + md = require('markdown-it')(); + illiterate = function(text) { + var out = []; + md.renderer.rules = { + code_block: function (tokens, idx) { + out.push(tokens[idx].content) } - if(item.type === 'code'){ - target && out[target].push(item.text); - } - }); - var defaultOut = out.default; - delete out.default; - var outArr = _.map(out, function(val, key){ - return { - filename: lexed.links['src://'+key].href, - content: val.join('\n') - }; - }); - outArr.default = defaultOut.join('\n'); - return outArr; + } + md.render(text) + return out.join(''); }; if (typeof exports !== 'undefined') { if (typeof module !== 'undefined' && module.exports) { diff --git a/package.json b/package.json index 463075b..61a639d 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { "name": "illiterate", - "version": "1.0.0", + "version": "1.1.0", "dependencies": { "lodash": "~3.4.0", - "marked": "~0.3.3" + "markdown-it": "^8.3.1" }, "description": "Extract code block from markdown à la coffeescript - but for any language", "main": "lib/illiterate.js", diff --git a/src/illiterate.js.md b/src/illiterate.js.md index 4128c19..6ccd565 100644 --- a/src/illiterate.js.md +++ b/src/illiterate.js.md @@ -37,55 +37,36 @@ Create self executing enclosure - convert function into expression by prefixing var root = this, illiterate; -Load dependencies... but how to handle this in the browser context..? +Load dependencies... but how to handle this in the browser context..!? - var _ = require('lodash'), - marked = require('marked'); + var _ = require('lodash'), + md = require('markdown-it')(); -Define main parse method, which accepts a string. +Define main parse method, which accepts a string - illiterate = function(text){ + illiterate = function(text) { -Create a variable to store output as it is built up from input files, set the target to default (where content without specified files is attached) and markdown lexer results. +Create a variable to store output as it is built up from input files - var out = { default: [] }, - target = 'default', - // from marked.js Inline-Level Grammar... - srcPattern = /^\[((?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*)\]\s*\[src:\/\/([^\]]*)\]/, - m, - lexed = marked.lexer(text, {}); + var out = []; ### Main loop -Pass the input text through a markdown parser, then reduce the tokens to extract only code blocks, joining with newlines, and pushing onto the output array. +We only want to extract code blocks, so remove other rules, and override `code_block` rule - _.each(lexed, function(item){ - if(!!item.text && item.text[0] === '[' && (m = item.text.match(srcPattern))) { - target = m[2]; - out[target] = out[target] || []; + md.renderer.rules = { + code_block: function (tokens, idx) { + out.push(tokens[idx].content) } - if(item.type === 'code'){ - target && out[target].push(item.text); - } - }); - -Post process main loop. + } - var defaultOut = out.default; - delete out.default; - var outArr = _.map(out, function(val, key){ - return { - filename: lexed.links['src://'+key].href, - content: val.join('\n') - }; - }); - outArr.default = defaultOut.join('\n'); +Run the render method, which will callback the `code_block` rule for each extracted block of code -### Output + md.render(text) Output extracted code blocks - return outArr; + return out.join(''); }; diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..4f4a4b9 --- /dev/null +++ b/yarn.lock @@ -0,0 +1,49 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +argparse@^1.0.7: + version "1.0.9" + resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.9.tgz#73d83bc263f86e97f8cc4f6bae1b0e90a7d22c86" + dependencies: + sprintf-js "~1.0.2" + +entities@~1.1.1: + version "1.1.1" + resolved "https://registry.yarnpkg.com/entities/-/entities-1.1.1.tgz#6e5c2d0a5621b5dadaecef80b90edfb5cd7772f0" + +linkify-it@^2.0.0: + version "2.0.3" + resolved "https://registry.yarnpkg.com/linkify-it/-/linkify-it-2.0.3.tgz#d94a4648f9b1c179d64fa97291268bdb6ce9434f" + dependencies: + uc.micro "^1.0.1" + +lodash@~3.4.0: + version "3.4.0" + resolved "https://registry.yarnpkg.com/lodash/-/lodash-3.4.0.tgz#7c39c1336faf5f96e6409f8355f26ea192f41821" + +markdown-it@^8.3.1: + version "8.3.1" + resolved "https://registry.yarnpkg.com/markdown-it/-/markdown-it-8.3.1.tgz#2f4b622948ccdc193d66f3ca2d43125ac4ac7323" + dependencies: + argparse "^1.0.7" + entities "~1.1.1" + linkify-it "^2.0.0" + mdurl "^1.0.1" + uc.micro "^1.0.3" + +marked@~0.3.3: + version "0.3.6" + resolved "https://registry.yarnpkg.com/marked/-/marked-0.3.6.tgz#b2c6c618fccece4ef86c4fc6cb8a7cbf5aeda8d7" + +mdurl@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/mdurl/-/mdurl-1.0.1.tgz#fe85b2ec75a59037f2adfec100fd6c601761152e" + +sprintf-js@~1.0.2: + version "1.0.3" + resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c" + +uc.micro@^1.0.1, uc.micro@^1.0.3: + version "1.0.3" + resolved "https://registry.yarnpkg.com/uc.micro/-/uc.micro-1.0.3.tgz#7ed50d5e0f9a9fb0a573379259f2a77458d50192"