Last active
January 17, 2017 21:56
-
-
Save vsemozhetbyt/d714bf98f4235a77db923e1c8744bc1b to your computer and use it in GitHub Desktop.
Extract JavaScript fragments from a markdown file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const fs = require('fs'); | |
const path = require('path'); | |
const rl = require('readline'); | |
const [, , mdFilePath] = process.argv; | |
const jsDirPath = path.join( | |
path.dirname(mdFilePath), | |
`${path.basename(mdFilePath)}.extracted-js` | |
); | |
fs.mkdirSync(jsDirPath); | |
const rli = rl.createInterface({ | |
input: fs.createReadStream(mdFilePath, 'utf8'), | |
}); | |
const jsFragments = []; | |
const jsFragmentStart = /^(\s*)```(?:js|javascript)\s*$/i; | |
const jsFragmentEnd = /^\s*```\s*$/; | |
let lineNumber = 0; | |
let inJsFragment = false; | |
let initialIndentation; | |
rli.on('line', (line) => { | |
lineNumber++; | |
if (jsFragmentStart.test(line) && !inJsFragment) { | |
inJsFragment = true; | |
initialIndentation = new RegExp(`^${line.match(jsFragmentStart)[1]}`); | |
jsFragments.push({ startLine: lineNumber + 1, endLine: undefined, js: '' }); | |
} else if (jsFragmentEnd.test(line) && inJsFragment) { | |
inJsFragment = false; | |
jsFragments[jsFragments.length - 1].endLine = lineNumber - 1; | |
} else if (inJsFragment) { | |
jsFragments[jsFragments.length - 1].js += `${line.replace(initialIndentation, '')}\n`; | |
} | |
}).on('close', () => { | |
const lastEndLineNumber = jsFragments[jsFragments.length - 1].endLine; | |
jsFragments.forEach((jsFragment, i) => { | |
jsFragment.startLine = padLeft(jsFragment.startLine, lastEndLineNumber); | |
jsFragment.endLine = padLeft(jsFragment.endLine, lastEndLineNumber); | |
const jsFileName = `${ | |
padLeft(i + 1, jsFragments.length) | |
}.${jsFragment.startLine}-${jsFragment.endLine}.js`; | |
fs.writeFileSync( | |
path.join(jsDirPath, jsFileName), | |
`\uFEFF'use strict'; | |
// ${ | |
jsFragment.startLine === jsFragment.endLine ? | |
jsFragment.startLine : | |
`${jsFragment.startLine} – ${jsFragment.endLine}` | |
} | |
${jsFragment.js}`, | |
'utf8' | |
); | |
}); | |
}); | |
function padLeft(padNumber, edgeNumber) { | |
return `${ | |
'0'.repeat(String(edgeNumber).length - String(padNumber).length) | |
}${padNumber}`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
md.extract-js.js markdown-file.md
.Extracted
.js
files will be created in themarkdown-file.md.extracted-js
directory next tomarkdown-file.md
. Each file has this name template:numberOfFragment.numberOfFirstLine-numberOfLastLine.js
.