Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,42 @@ const fs = require('fs')
const path = require('path')
const glob = require('glob')
const toc = require('markdown-toc')
const inflection = require('inflection')

const dir = process.argv[2]
const index = process.argv[3]

if (!dir) {
console.log('\nUsage: \nmarkdown-index directory/fulla/markdowns\n')
console.log('\nUsage: \nmarkdown-index directory/full/of/markdowns\n')
process.exit(1)
}

glob(dir + '/**/*.md', function (err, files) {
glob(`${dir}/**/*.md`, (err, files) => {
if (err) throw err
var tables = files.map(function (file) {
const tables = files.map(file => {
if (file.match('index.md')) return
if (file.match('README.md')) return
if (file.match('node_modules')) return

var filenameSlug = toc.slugify(path.parse(file).name)
var originalLinkify = toc.linkify
var table = toc(fs.readFileSync(file, 'utf8'), {
filter: function(str, ele, arr) {
// Skip top-level elements if their slug matches a slug of the filename.
return ele.level != 1 || ele.slug != filenameSlug
},
linkify: function(tok, text, slug, opts) {
const filenameSlug = toc.slugify(path.parse(file).name)
const originalLinkify = toc.linkify
const table = toc(fs.readFileSync(file, 'utf8'), {
filter: (str, ele, arr) => ele.level !== 1 || ele.slug !== filenameSlug, // Skip top-level elements if their slug matches a slug of the filename.
linkify: (tok, text, slug, opts) => {
// Use empty options arg to avoid infinite recursion.
var tok = originalLinkify(tok, text, slug, {})
const newTok = originalLinkify(tok, text, slug, {})
// Prepend filename to links.
tok.content = tok.content.replace('#', file + '#')
return tok
newTok.content = newTok.content.replace('#', `${file}#`)
return newTok
}
})

// Add filename as a heading.
return '### [' + filenameSlug + '](' + file + ')\n\n' + table.content
return `#### [${path.parse(file).name}](${file})\n${table.content}\n`
})

process.stdout.write(tables.join('\n\n'))
return tables.join('\n\n')
if (index) {
fs.writeFile(index, tables.join(''), 'utf8', err => (err ? console.log(err) : null))
} else {
process.stdout.write(tables.join('\n'))
}
})