Skip to content

adopt @sindresorhus/slugify #1562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2024
Merged
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-virtual": "^3.0.2",
"@sindresorhus/slugify": "^2.2.1",
"acorn": "^8.11.2",
"acorn-walk": "^8.3.0",
"ci-info": "^4.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {createHash} from "node:crypto";
import type {Stats} from "node:fs";
import {readFile, stat} from "node:fs/promises";
import {join} from "node:path/posix";
import slugify from "@sindresorhus/slugify";
import wrapAnsi from "wrap-ansi";
import type {BuildEffects, BuildManifest, BuildOptions} from "./build.js";
import {FileBuildEffects, build} from "./build.js";
Expand All @@ -25,7 +26,6 @@ import type {
} from "./observableApiClient.js";
import type {ConfigEffects, DeployConfig} from "./observableApiConfig.js";
import {defaultEffects as defaultConfigEffects, getDeployConfig, setDeployConfig} from "./observableApiConfig.js";
import {slugify} from "./slugify.js";
import {Telemetry} from "./telemetry.js";
import type {TtyEffects} from "./tty.js";
import {bold, defaultEffects as defaultTtyEffects, faint, inverse, link, underline, yellow} from "./tty.js";
Expand Down
3 changes: 2 additions & 1 deletion src/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable import/no-named-as-default-member */
import {createHash} from "node:crypto";
import slugify from "@sindresorhus/slugify";
import {transformSync} from "esbuild";
import he from "he";
import MarkdownIt from "markdown-it";
Expand Down Expand Up @@ -226,7 +227,7 @@ export function createMarkdownIt({
} = {}): MarkdownIt {
const md = MarkdownIt({html: true, linkify, typographer, quotes});
if (linkify) md.linkify.set({fuzzyLink: false, fuzzyEmail: false});
md.use(MarkdownItAnchor);
md.use(MarkdownItAnchor, {slugify: (s) => slugify(s)});
md.inline.ruler.push("placeholder", transformPlaceholderInline);
md.core.ruler.after("inline", "placeholder", transformPlaceholderCore);
md.renderer.rules.placeholder = makePlaceholderRenderer();
Expand Down
8 changes: 0 additions & 8 deletions src/slugify.ts

This file was deleted.

2 changes: 1 addition & 1 deletion test/output/build/config/closed/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
</aside>
<div id="observablehq-center">
<main id="observablehq-main" class="observablehq">
<h1 id="a-page%E2%80%A6" tabindex="-1"><a class="observablehq-header-anchor" href="#a-page%E2%80%A6">A page…</a></h1>
<h1 id="a-page" tabindex="-1"><a class="observablehq-header-anchor" href="#a-page">A page…</a></h1>
<p>in a closed subsection.</p>
</main>
<footer id="observablehq-footer">
Expand Down
10 changes: 5 additions & 5 deletions test/output/build/config/toc-override.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@
<nav>
<div>dollar&amp;pound</div>
<ol>
<li class="observablehq-secondary-link"><a href="#h2%3A-section-2">H2: Section 2</a></li>
<li class="observablehq-secondary-link"><a href="#h2-section-2">H2: Section 2</a></li>
</ol>
</nav>
</aside>
<div id="observablehq-center">
<main id="observablehq-main" class="observablehq">
<h1 id="h1%3A-section" tabindex="-1"><a class="observablehq-header-anchor" href="#h1%3A-section">H1: Section</a></h1>
<h2 id="h2%3A-section-1" tabindex="-1"><a class="observablehq-header-anchor" href="#h2%3A-section-1">H2: Section 1</a></h2>
<h1 id="h1-section" tabindex="-1"><a class="observablehq-header-anchor" href="#h1-section">H1: Section</a></h1>
<h2 id="h2-section-1" tabindex="-1"><a class="observablehq-header-anchor" href="#h2-section-1">H2: Section 1</a></h2>
<p>Some text here</p>
<h2 id="h2%3A-section-2" tabindex="-1"><a class="observablehq-header-anchor" href="#h2%3A-section-2">H2: Section 2</a></h2>
<h2 id="h2-section-2" tabindex="-1"><a class="observablehq-header-anchor" href="#h2-section-2">H2: Section 2</a></h2>
<p>Some text here again</p>
<h3 id="h3%3A-section-1" tabindex="-1"><a class="observablehq-header-anchor" href="#h3%3A-section-1">H3: Section 1</a></h3>
<h3 id="h3-section-1" tabindex="-1"><a class="observablehq-header-anchor" href="#h3-section-1">H3: Section 1</a></h3>
</main>
<footer id="observablehq-footer">
<div>Built with <a href="https://observablehq.com/" target="_blank" rel="noopener noreferrer">Observable</a> on <a title="2024-01-10T16:00:00">Jan 10, 2024</a>.</div>
Expand Down
14 changes: 7 additions & 7 deletions test/output/build/config/toc.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,20 @@
<nav>
<div>On this page</div>
<ol>
<li class="observablehq-secondary-link"><a href="#h2%3A-section-2">H2: Section 2</a></li>
<li class="observablehq-secondary-link"><a href="#h2-%3Cscript%3Ealert(1)%3C%2Fscript%3E-not-nice">H2 &lt;script&gt;alert(1)&lt;/script&gt; not nice</a></li>
<li class="observablehq-secondary-link"><a href="#h2-section-2">H2: Section 2</a></li>
<li class="observablehq-secondary-link"><a href="#h2-script-alert-1-script-not-nice">H2 &lt;script&gt;alert(1)&lt;/script&gt; not nice</a></li>
</ol>
</nav>
</aside>
<div id="observablehq-center">
<main id="observablehq-main" class="observablehq">
<h1 id="h1%3A-section" tabindex="-1"><a class="observablehq-header-anchor" href="#h1%3A-section">H1: Section</a></h1>
<h2 id="h2%3A-section-1" tabindex="-1"><a class="observablehq-header-anchor" href="#h2%3A-section-1">H2: Section 1</a></h2>
<h1 id="h1-section" tabindex="-1"><a class="observablehq-header-anchor" href="#h1-section">H1: Section</a></h1>
<h2 id="h2-section-1" tabindex="-1"><a class="observablehq-header-anchor" href="#h2-section-1">H2: Section 1</a></h2>
<p>Some text here</p>
<h2 id="h2%3A-section-2" tabindex="-1"><a class="observablehq-header-anchor" href="#h2%3A-section-2">H2: Section 2</a></h2>
<h2 id="h2-section-2" tabindex="-1"><a class="observablehq-header-anchor" href="#h2-section-2">H2: Section 2</a></h2>
<p>Some text here again</p>
<h3 id="h3%3A-section-1" tabindex="-1"><a class="observablehq-header-anchor" href="#h3%3A-section-1">H3: Section 1</a></h3>
<h2 id="h2-%3Cscript%3Ealert(1)%3C%2Fscript%3E-not-nice" tabindex="-1"><a class="observablehq-header-anchor" href="#h2-%3Cscript%3Ealert(1)%3C%2Fscript%3E-not-nice">H2 &lt;script&gt;alert(1)&lt;/script&gt; not nice</a></h2>
<h3 id="h3-section-1" tabindex="-1"><a class="observablehq-header-anchor" href="#h3-section-1">H3: Section 1</a></h3>
<h2 id="h2-script-alert-1-script-not-nice" tabindex="-1"><a class="observablehq-header-anchor" href="#h2-script-alert-1-script-not-nice">H2 &lt;script&gt;alert(1)&lt;/script&gt; not nice</a></h2>
</main>
<footer id="observablehq-footer">
<div>Built with <a href="https://observablehq.com/" target="_blank" rel="noopener noreferrer">Observable</a> on <a title="2024-01-10T16:00:00">Jan 10, 2024</a>.</div>
Expand Down
2 changes: 1 addition & 1 deletion test/output/build/pager/sub/page1..10.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
</aside>
<div id="observablehq-center">
<main id="observablehq-main" class="observablehq">
<h1 id="page-1..10" tabindex="-1"><a class="observablehq-header-anchor" href="#page-1..10">page 1..10</a></h1>
<h1 id="page-1-10" tabindex="-1"><a class="observablehq-header-anchor" href="#page-1-10">page 1..10</a></h1>
</main>
<footer id="observablehq-footer">
<div>Built with <a href="https://observablehq.com/" target="_blank" rel="noopener noreferrer">Observable</a> on <a title="2024-01-10T16:00:00">Jan 10, 2024</a>.</div>
Expand Down
2 changes: 1 addition & 1 deletion test/output/hello-world.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<h1 id="hello%2C-world!" tabindex="-1">Hello, world!</h1>
<h1 id="hello-world" tabindex="-1">Hello, world!</h1>
<p>This is a test.</p>
2 changes: 1 addition & 1 deletion test/output/setext.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<h2 id="%60hello" tabindex="-1">`Hello <observablehq-loading></observablehq-loading><!--:6212702c:--></h2>
<h2 id="hello" tabindex="-1">`Hello <observablehq-loading></observablehq-loading><!--:6212702c:--></h2>
<p>`</p>
2 changes: 1 addition & 1 deletion test/output/tex-expression.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<h1 id="hello%2C" tabindex="-1">Hello, <observablehq-loading></observablehq-loading><!--:16447e3a:--></h1>
<h1 id="hello" tabindex="-1">Hello, <observablehq-loading></observablehq-loading><!--:16447e3a:--></h1>
<p>My favorite equation is <observablehq-loading></observablehq-loading><!--:828eccc1:-->.</p>
11 changes: 8 additions & 3 deletions test/slugify-test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "node:assert";
import {slugify} from "../src/slugify.js";
import slugify from "@sindresorhus/slugify";

describe("slugify", () => {
it("works", async () => {
Expand All @@ -11,8 +11,13 @@ describe("slugify", () => {
it("removes duplicatehyphens", async () => {
assert.equal(slugify("hello--world"), "hello-world");
});
it("handles apostrophes", async () => {
assert.equal(slugify("hello'world"), "helloworld");
it("handles punctuation", async () => {
assert.equal(slugify("u.s. map"), "u-s-map");
assert.equal(slugify("hello'world"), "hello-world");
assert.equal(slugify("hello''world"), "hello-world");
assert.equal(slugify("hello…"), "hello");
assert.equal(slugify("'"), "");
assert.equal(slugify("…"), "");
});
it("handles empty strings", async () => {
assert.equal(slugify(""), "");
Expand Down
25 changes: 20 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,21 @@
resolved "https://registry.yarnpkg.com/@rollup/rollup-win32-x64-msvc/-/rollup-win32-x64-msvc-4.19.1.tgz#5f2c40d3f1b53ede80fb4e6964f840c0f8936832"
integrity sha512-2bIrL28PcK3YCqD9anGxDxamxdiJAxA+l7fWIwM5o8UqNy1t3d1NdAweO2XhA0KTDJ5aH1FsuiT5+7VhtHliXg==

"@sindresorhus/slugify@^2.2.1":
version "2.2.1"
resolved "https://registry.yarnpkg.com/@sindresorhus/slugify/-/slugify-2.2.1.tgz#fa2e2e25d6e1e74a2eeb5e2c37f5ccc516ed2c4b"
integrity sha512-MkngSCRZ8JdSOCHRaYd+D01XhvU3Hjy6MGl06zhOk614hp9EOAp5gIkBeQg7wtmxpitU6eAL4kdiRMcJa2dlrw==
dependencies:
"@sindresorhus/transliterate" "^1.0.0"
escape-string-regexp "^5.0.0"

"@sindresorhus/transliterate@^1.0.0":
version "1.6.0"
resolved "https://registry.yarnpkg.com/@sindresorhus/transliterate/-/transliterate-1.6.0.tgz#2309fff65a868047e6d2dd70dec747c5b36a8327"
integrity sha512-doH1gimEu3A46VX6aVxpHTeHrytJAG6HgdxntYnCFiIFHEM/ZGpG8KiZGBChchjQmG0XFIBL552kBTjVcMZXwQ==
dependencies:
escape-string-regexp "^5.0.0"

"@types/chai@4":
version "4.3.16"
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.3.16.tgz#b1572967f0b8b60bf3f87fe1d854a5604ea70c82"
Expand Down Expand Up @@ -1669,6 +1684,11 @@ escape-string-regexp@^4.0.0:
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz#14ba83a5d373e3d311e5afca29cf5bfad965bf34"
integrity sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==

escape-string-regexp@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-5.0.0.tgz#4683126b500b61762f2dbebace1806e8be31b1c8"
integrity sha512-/veY75JbMK4j1yjvuUxuVsiS/hr/4iHs9FTT6cgTexxdE0Ly/glccBAkloH/DofkjRbZU3bnoj38mOmhkZ0lHw==

eslint-config-prettier@^9.1.0:
version "9.1.0"
resolved "https://registry.yarnpkg.com/eslint-config-prettier/-/eslint-config-prettier-9.1.0.tgz#31af3d94578645966c082fcb71a5846d3c94867f"
Expand Down Expand Up @@ -2502,11 +2522,6 @@ is-unicode-supported@^0.1.0:
resolved "https://registry.yarnpkg.com/is-unicode-supported/-/is-unicode-supported-0.1.0.tgz#3f26c76a809593b52bfa2ecb5710ed2779b522a7"
integrity sha512-knxG2q4UC3u8stRGyAVJCOdxFmv5DZiRcdlIaAQXAbSfJya+OhopNotLQrstBhququ4ZpuKbDc/8S6mgXgPFPw==

is-unicode-supported@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/is-unicode-supported/-/is-unicode-supported-1.3.0.tgz#d824984b616c292a2e198207d4a609983842f714"
integrity sha512-43r2mRvz+8JRIKnWJ+3j8JtjRKZ6GmjzfaE/qiBJnikNnYv/6bagRJ1kUhNk8R5EX/GkobD+r+sfxCPJsiKBLQ==

is-weakref@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/is-weakref/-/is-weakref-1.0.2.tgz#9529f383a9338205e89765e0392efc2f100f06f2"
Expand Down