Heading ids drop the hyphen when a title contains " - " or "--" (kg-utils slugify)
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
- Tech stack
- javascript, node.js, typescript
- 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
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
- 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)));
- Actual output:
setuplinux
nodejsintro
xy
ab
"<h2 id=\"setuplinux\">Setup - Linux</h2>\n"
"<h2 id=\"setuplinux\">Setup - Linux</h2>"
- Expected output:
setup-linux,nodejs-intro,x-y,a-b, andid="setup-linux"on both rendered<h2>elements so#setup-linuxanchor 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
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from TryGhost/Ghost
-
needs:triage
Difficulty 1/5 Under an hour Newbie friendliness 92/100
Maintainers usually reply within 1 day
-
needs:triage
Difficulty 2/5 1-3 hours Newbie friendliness 63/100
Maintainers usually reply within 1 day
-
needs:triage
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
HTML import of product cards mistakes description links for the product buttonPossibly taken @JoaoMarcos44 claimed this 14 days ago. Openaffects:api affects:editor affects:import/upgrade bug
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
TryGhost/Ghost#32429 · 2 comments ·
Maintainers usually reply within 1 day
-
[a11y] Portal toggles for email preferences don't have accessible labelsMay be free again A pull request for this issue was closed without being merged. Openaffects:a11y
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
TryGhost/Ghost#29667 · 1 comment ·
Maintainers usually reply within 1 day
Similar issues
-
area:cli area:security bug priority:medium
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
"code source" and "numerique.gouv" links: "new window" mention duplicated in the accessible nameOpenaccessibility
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
numerique-gouv/lasuite-landingpage#315 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100
HyeokjaeChoi/vaultme#585 ·
Maintainers usually reply within 1 day
-
enhancement priority/P2
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
beettlle/pi-smart-router#200 ·
-
Folder workspaces disappear from sidebar in manual groups without a base folderPossibly taken @bbingz claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
stablyai/orca#42865 · 1 comment ·
Maintainers usually reply within 1 day