Skip to content

Instantly share code, notes, and snippets.

@bclinkinbeard
Created November 29, 2017 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bclinkinbeard/cd8e815d5850be350f349ff32c9dd1f9 to your computer and use it in GitHub Desktop.
Save bclinkinbeard/cd8e815d5850be350f349ff32c9dd1f9 to your computer and use it in GitHub Desktop.
Extract all the non-png links from a directory of Markdown files
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