Skip to content

Commit

Permalink
Specify markdown-toc as a dependency
Browse files Browse the repository at this point in the history
Previously, the `entry` for the markdown-toc hook was `npx md-toc`,
which was meant to call the `pre-commmit-markdown-toc` script, via the
mapping in the `bin` section of `package.json`. The script then looked
for the `markdown-toc` executable in the
`~/.cache/pre-commit/{reponame}/node_modules/.bin/markdown-toc`
directory, but would return an error because it couldn't find it. It
doesn't get automatically installed there, even when specified as an
additional dependency.

To add to the confusion, if you had the `md-toc` package installed
locally, that's what would be called instead of the
`pre-commmit-markdown-toc` script, which made the error go away, but
wouldn't actually generate a TOC.

The solution in 3 steps:

1. Use an `entry` name that doesn't match an existing npm package.
2. Define `markdown-toc` as an additional dependency so that it gets
automatically installed.
3. Instead of looking for the `markdown-toc` executable in the
`pre-commit` directory, just call it directly.
  • Loading branch information
monfresh committed Jan 29, 2021
1 parent 66fb11b commit 504858c
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
- id: markdown-toc
name: Generate a Table of Contents in Markdown files
description: Generate a Table of Contents in Markdown files
entry: npx md-toc
entry: markdowntoc
language: node
types: ["file", "markdown"]
additional_dependencies: ["markdown-toc"]

- id: mdspell
name: Run spellcheck
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"name": "pre-commit-hooks",
"private": true,
"bin": {
"md-toc": "./pre-commit-markdown-toc",
"markdowntoc": "./pre-commit-markdown-toc",
"adr": "./pre-commit-gen-docs"
},
"scripts": {},
Expand Down
6 changes: 1 addition & 5 deletions pre-commit-markdown-toc
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@

set -eu -o pipefail

# Get the directory of this file inside pre-commit cache
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
MDTOC_BIN="${DIR}/../../node_modules/.bin/markdown-toc"

for filename in "$@"; do
# Using backticks in this appended comment seems to make the script
# indicated run after a 3/5 update to markdown-toc 1.2.0;
# stick with quotes for now.
regen=$'\n\n<!-- Regenerate with "pre-commit run -a markdown-toc" -->'

node "${MDTOC_BIN}" --bullets='*' --append="${regen}" -i "${filename}"
markdown-toc --bullets='*' --append="${regen}" -i "${filename}"
# yarn run --silent markdown-toc --bullets='*' --append="${regen}" -i "${filename}"
done

0 comments on commit 504858c

Please sign in to comment.