Skip to content
Open
Show file tree
Hide file tree
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
48 changes: 48 additions & 0 deletions .github/scripts/sync-version-on-commit.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env node

import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

console.info('Syncing all version references...');

// Read version from package.json
const packageJsonPath = path.join(__dirname, '..', '..', 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
const version = packageJson.version;

console.info(`Version from package.json: ${version}`);

// File paths
const markdownlintJsPath = path.join(__dirname, '..', '..', 'markdownlint.js');
const readmePath = path.join(__dirname, '..', '..', 'README.md');
const preCommitHooksPath = path.join(__dirname, '..', '..', '.pre-commit-hooks.yaml');

// Update markdownlint.js
let markdownlintJs = fs.readFileSync(markdownlintJsPath, 'utf8');
markdownlintJs = markdownlintJs.replace(
/const version = '[^']+';/,
`const version = '${version}';`
);
fs.writeFileSync(markdownlintJsPath, markdownlintJs);

// Update README.md
let readme = fs.readFileSync(readmePath, 'utf8');
readme = readme.replace(
/rev: v[0-9]+\.[0-9]+\.[0-9]+/,
`rev: v${version}`
);
fs.writeFileSync(readmePath, readme);

// Update .pre-commit-hooks.yaml
let preCommitHooks = fs.readFileSync(preCommitHooksPath, 'utf8');
preCommitHooks = preCommitHooks.replace(
/entry: ghcr\.io\/igorshubovych\/markdownlint-cli(?::[^\s]+)?(\s|$)/g,
`entry: ghcr.io/igorshubovych/markdownlint-cli:v${version}$1`
);
fs.writeFileSync(preCommitHooksPath, preCommitHooks);

console.info('✓ Version sync complete');
28 changes: 28 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Pre-commit

on:
push:
branches:
- master
pull_request:

jobs:
pre-commit:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v6

- name: Setup Node.js
uses: actions/setup-node@v6
with:
node-version: lts/*

- name: Setup Python
uses: actions/setup-python@v6
with:
python-version: '3.x'

- name: Run pre-commit
uses: pre-commit/action@v3.0.1
11 changes: 11 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: local
hooks:
- id: sync-version
name: Sync version references
entry: node .github/scripts/sync-version-on-commit.mjs
language: system
files: ^(package\.json|markdownlint\.js|README\.md|\.pre-commit-hooks\.yaml)$
pass_filenames: false
4 changes: 2 additions & 2 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
- id: markdownlint-docker
name: markdownlint-docker
description: "Checks the style of Markdown/Commonmark files."
entry: ghcr.io/igorshubovych/markdownlint-cli
entry: ghcr.io/igorshubovych/markdownlint-cli:v0.46.0
language: docker_image
types: [markdown]
minimum_pre_commit_version: 0.15.0
- id: markdownlint-fix-docker
name: markdownlint-fix-docker
description: "Fixes the style of Markdown/Commonmark files."
entry: ghcr.io/igorshubovych/markdownlint-cli --fix
entry: ghcr.io/igorshubovych/markdownlint-cli:v0.46.0 --fix
language: docker_image
types: [markdown]
minimum_pre_commit_version: 0.15.0
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,56 @@ To run `markdownlint-cli` as part of a [pre-commit][pre-commit] workflow, add so

> Depending on the environment this workflow runs in, it may be necessary to [override the language version of Node.js used by pre-commit][pre-commit-version].

## Contributing

### Setting up local development

This project uses [pre-commit][pre-commit] hooks to maintain version consistency across multiple files. To set up your local development environment:

#### macOS/Linux

```bash
# Install pre-commit (if not already installed)
pip install pre-commit
# or
brew install pre-commit

# Install the git hook scripts
pre-commit install
```

#### Windows

```powershell
# Install pre-commit via pip
pip install pre-commit

# Install the git hook scripts
pre-commit install
```

#### What the pre-commit hooks do

- **Version sync**: Automatically ensures version references in `markdownlint.js`, `README.md`, and `.pre-commit-hooks.yaml` stay in sync with `package.json`

When you commit changes to any version-related files, the hooks will:

1. Read the version from `package.json`
2. Update all other files to match
3. If files were modified, the commit will fail with a message asking you to review and stage the changes
4. Review the changes with `git diff` and re-add them with `git add`
5. Retry your commit

#### Running hooks manually

```bash
# Run all hooks on all files
pre-commit run --all-files

# Run hooks only on staged files
pre-commit run
```

## Related

- [markdownlint][markdownlint] - API for this module
Expand Down
2 changes: 1 addition & 1 deletion markdownlint.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import jsonpointer from 'jsonpointer';
const require = Module.createRequire(import.meta.url);
const options = program.opts();
// The following two values are copied from package.json (and validated by tests)
const version = '0.46.0';
const version = '0.46.1';
const description = 'MarkdownLint Command Line Interface';

function markdownItFactory() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "markdownlint-cli",
"version": "0.46.0",
"version": "0.46.1",
"description": "MarkdownLint Command Line Interface",
"type": "module",
"main": "markdownlint.js",
Expand Down
Loading