Created
November 29, 2017 17:10
-
-
Save bclinkinbeard/cd8e815d5850be350f349ff32c9dd1f9 to your computer and use it in GitHub Desktop.
Extract all the non-png links from a directory of Markdown files
This file contains hidden or 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
const markdownLinkExtractor = require('markdown-link-extractor') | |
const fs = require('fs') | |
const { join } = require('path') | |
const dir = join(__dirname, 'chapters') | |
const files = fs.readdirSync(dir) | |
const titles = files.map( | |
file => file.substr(0, file.lastIndexOf('.')).split(' - ')[1], | |
) | |
const getMarkdown = f => fs.readFileSync(join(dir, f)).toString() | |
fs.writeFileSync( | |
join(__dirname, 'links.md'), | |
files | |
.filter(f => !f.startsWith('.')) | |
.map((f, i) => { | |
const links = markdownLinkExtractor(getMarkdown(f)) | |
const fLinks = links.filter(url => url.substr(-3) !== 'png') | |
if (!fLinks.length) return null | |
return [`## ${titles[i]}`, fLinks.join('\n')].join('\n') | |
}) | |
.filter(s => s !== null) | |
.join('\n\n'), | |
'utf8', | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment