Skip to content

Commit fc6cb34

Browse files
committed
feat: deduce prefixUrl, slugify headings
1 parent b5389fb commit fc6cb34

File tree

6 files changed

+14
-11
lines changed

6 files changed

+14
-11
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"html-minifier": "^4.0.0",
2020
"is-relative-url": "^3.0.0",
2121
"markdown-it": "^12.0.2",
22+
"markdown-it-anchor": "^6.0.1",
2223
"prettier": "^2.2.1",
2324
"prettier-plugin-svelte": "^1.4.1",
2425
"prism-svelte": "^0.4.7",

src/preprocessReadme.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { walk, parse } from "svelte/compiler";
22
import Markdown from "markdown-it";
3+
import markdownItAnchor from "markdown-it-anchor";
34
import prettier from "prettier";
45
import Prism from "prismjs";
56
import "prismjs/components/prism-bash";
@@ -14,6 +15,8 @@ const aliases = {
1415
let md;
1516

1617
export function preprocessReadme(opts) {
18+
const prefixUrl = opts.prefixUrl || `${opts.homepage}/tree/master/`;
19+
1720
if (!md) {
1821
md = new Markdown({
1922
html: true,
@@ -45,6 +48,8 @@ export function preprocessReadme(opts) {
4548
}
4649
},
4750
});
51+
52+
md.use(markdownItAnchor);
4853
}
4954

5055
return {
@@ -59,11 +64,11 @@ export function preprocessReadme(opts) {
5964

6065
walk(ast, {
6166
enter(node, parent) {
62-
if (opts.prefixUrl && node.type === "Attribute" && node.name === "href") {
67+
if (node.type === "Attribute" && node.name === "href") {
6368
const value = node.value[0];
6469

6570
if (value && !value.raw.startsWith("#") && isRelativeUrl(value.raw)) {
66-
const relative_path = new URL(value.raw, opts.prefixUrl).href;
71+
const relative_path = new URL(value.raw, prefixUrl).href;
6772
result = result.replace(value.raw, relative_path);
6873
cursor += relative_path.length - value.raw.length;
6974
}

test/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,5 @@
1919
"serve": "^11.3.2",
2020
"svelte": "^3.29.7"
2121
},
22-
"dependencies": {
23-
"github-markdown-css": "^4.0.0"
24-
}
22+
"homepage": "https://github.com/metonym/svelte-readme"
2523
}

test/rollup.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export default () => {
1818
port: 5000,
1919
}),
2020
],
21-
prefixUrl: "https://github.com/metonym/svelte-readme/tree/master/test/",
2221
});
2322

2423
return ["es", "umd"].map((format) => {

test/yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,6 @@ get-stream@^3.0.0:
305305
resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-3.0.0.tgz#8e943d1358dc37555054ecbe2edb05aa174ede14"
306306
integrity sha1-jpQ9E1jcN1VQVOy+LtsFqhdO3hQ=
307307

308-
github-markdown-css@^4.0.0:
309-
version "4.0.0"
310-
resolved "https://registry.yarnpkg.com/github-markdown-css/-/github-markdown-css-4.0.0.tgz#be9f4caf7a389228d4c368336260ffc909061f35"
311-
integrity sha512-mH0bcIKv4XAN0mQVokfTdKo2OD5K8WJE9+lbMdM32/q0Ie5tXgVN/2o+zvToRMxSTUuiTRcLg5hzkFfOyBYreg==
312-
313308
has-flag@^3.0.0:
314309
version "3.0.0"
315310
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-3.0.0.tgz#b5d454dc2199ae225699f3467e5a07f3b955bafd"

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,11 @@ lower-case@^1.1.1:
309309
resolved "https://registry.yarnpkg.com/lower-case/-/lower-case-1.1.4.tgz#9a2cabd1b9e8e0ae993a4bf7d5875c39c42e8eac"
310310
integrity sha1-miyr0bno4K6ZOkv31YdcOcQujqw=
311311

312+
markdown-it-anchor@^6.0.1:
313+
version "6.0.1"
314+
resolved "https://registry.yarnpkg.com/markdown-it-anchor/-/markdown-it-anchor-6.0.1.tgz#f54b78757d0b61455faf8ced775b39498bd6d6f9"
315+
integrity sha512-8qX4r5R6AtXla9HKCouEQ40inw69O5jR4VUXlZySsBLxIXlsJ3Yi9JV6JWPU4ZdA8jWTGDDJjJYNLwQ0W4jCag==
316+
312317
markdown-it@^12.0.2:
313318
version "12.0.2"
314319
resolved "https://registry.yarnpkg.com/markdown-it/-/markdown-it-12.0.2.tgz#4401beae8df8aa2221fc6565a7188e60a06ef0ed"

0 commit comments

Comments
 (0)