Heading ids drop the hyphen when a title contains " - " or "--" (kg-utils slugify)

Open Beginner friendly
#32,919 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
85/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Domain
backend

Research direction

The bug is in slugify in koenig/kg-utils (src/slugify.ts, the 4.0+ branch), where .replace(/^-|-{2,}|-$/g, '') deletes runs of hyphens instead of collapsing them to '-'. Start by running the repro for 'Setup - Linux' and 'a--b' from the issue, then fix the replacement and add cases to test/slugify.test.ts. Done when those inputs slugify to 'setup-linux' and 'a-b' and the existing tests still pass.

Written by the indexing model from the issue text.

Description

needs:triage
Issue Summary

slugify in @tryghost/kg-utils (used to generate heading id attributes in @tryghost/kg-lexical-html-renderer and @tryghost/kg-markdown-html-renderer) deletes runs of two or more hyphens instead of collapsing them to -. Any heading with a spaced hyphen (" - ") or double hyphen ("--") loses the separator completely:

  • "Setup - Linux" becomes "setuplinux" instead of "setup-linux"
  • "node.js - intro" becomes "nodejsintro" instead of "nodejs-intro"
  • "a--b" becomes "ab" instead of "a-b"

In koenig/kg-utils/src/slugify.ts, the 4.0+ branch converts whitespace with .replace(/\s+/g, '-') and then runs .replace(/^-|-{2,}|-$/g, ''), which replaces -{2,} with '' rather than '-'. The unit test in koenig/kg-utils/test/slugify.test.ts (removes leading/trailing "-" and collapses "-" groups) passes only because its input (' \ttest one two! \t') has no literal hyphen next to whitespace, so \s+ collapses the spaces to a single - before -{2,} runs.

Steps to Reproduce
  1. Create a post with a normal Lexical heading or Markdown card heading ## Setup - Linux, or run:
npm install @tryghost/[email protected] @tryghost/[email protected] @tryghost/[email protected]
node repro.cjs

repro.cjs:

const {slugify} = require('@tryghost/kg-utils');
const {render: renderMarkdown} = require('@tryghost/kg-markdown-html-renderer');
const {LexicalHTMLRenderer} = require('@tryghost/kg-lexical-html-renderer');

console.log(slugify('Setup - Linux'));
console.log(slugify('node.js - intro'));
console.log(slugify('x -y'));
console.log(slugify('a--b'));

console.log(JSON.stringify(renderMarkdown('## Setup - Linux')));

const lexical = new LexicalHTMLRenderer();
lexical.render(JSON.stringify({
    root: {
        children: [{
            children: [{detail: 0, format: 0, mode: 'normal', style: '', text: 'Setup - Linux', type: 'text', version: 1}],
            direction: 'ltr', format: '', indent: 0, type: 'heading', version: 1, tag: 'h2'
        }],
        direction: 'ltr', format: '', indent: 0, type: 'root', version: 1
    }
})).then(html => console.log(JSON.stringify(html)));
  1. Actual output:
setuplinux
nodejsintro
xy
ab
"<h2 id=\"setuplinux\">Setup - Linux</h2>\n"
"<h2 id=\"setuplinux\">Setup - Linux</h2>"
  1. Expected output: setup-linux, nodejs-intro, x-y, a-b, and id="setup-linux" on both rendered <h2> elements so #setup-linux anchor links resolve.
Ghost Version

6.77.0 / main c2617d0 (@tryghost/kg-utils 1.1.9, @tryghost/kg-lexical-html-renderer 1.5.3, @tryghost/kg-markdown-html-renderer 7.2.11)

Node.js Version

26.13.0

How did you install Ghost?

npm packages on Linux x86_64 (reproduced with the published @tryghost/kg-* packages and main c2617d0)

Database type

Other

Browser & OS version

n/a

Relevant log / error output

n/a

Code of Conduct
  • I agree to be friendly and polite to people in this repository

Credit: https://ucsbplse.github.io/

Dominant language
TypeScript
Stars
55.8k
Forks
12.1k
Avg merge
1d 5h
Merged PRs (30d)
617

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from TryGhost/Ghost

All issues in TryGhost/Ghost

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.