From 451808da51cd773d0fe795658ccebe690932251c Mon Sep 17 00:00:00 2001 From: jer3m01 Date: Thu, 24 Oct 2024 12:50:34 +0200 Subject: [PATCH] feat: issue autolink --- dev/app.config.ts | 1 + dev/src/routes/index.mdx | 2 ++ docs/app.config.ts | 1 + docs/src/routes/guide/dev.mdx | 2 ++ package.json | 1 + pnpm-lock.yaml | 10 ++++++++++ src/config.ts | 7 ++++++- src/remark-plugins.ts | 31 +++++++++++++++++++++++++++++++ 8 files changed, 54 insertions(+), 1 deletion(-) diff --git a/dev/app.config.ts b/dev/app.config.ts index 0a35c0d..13e349b 100644 --- a/dev/app.config.ts +++ b/dev/app.config.ts @@ -21,6 +21,7 @@ export default defineConfig( github: "https://github.com/kobaltedev/solidbase", discord: "https://discord.com/invite/solidjs", }, + issueAutolink: "https://github.com/kobaltedev/solidbase/issues/:issue", search: { provider: "algolia", options: { diff --git a/dev/src/routes/index.mdx b/dev/src/routes/index.mdx index ab2b5d3..be017a6 100644 --- a/dev/src/routes/index.mdx +++ b/dev/src/routes/index.mdx @@ -60,6 +60,8 @@ www.example.com [Link internal /about](/about) +#20 also #4 but no \\#20 + A note[^1] [^1]: Big note. diff --git a/docs/app.config.ts b/docs/app.config.ts index d7398ba..57211e1 100644 --- a/docs/app.config.ts +++ b/docs/app.config.ts @@ -21,6 +21,7 @@ export default defineConfig( github: "https://github.com/kobaltedev/solidbase", discord: "https://discord.com/invite/solidjs", }, + issueAutolink: "https://github.com/kobaltedev/solidbase/issues/:issue", lang: "en", locales: { fr: { diff --git a/docs/src/routes/guide/dev.mdx b/docs/src/routes/guide/dev.mdx index 6e1bdc7..c7f7d49 100644 --- a/docs/src/routes/guide/dev.mdx +++ b/docs/src/routes/guide/dev.mdx @@ -64,6 +64,8 @@ www.example.com [Link internal /about](/about) +#20 also #4 but no \\#20 + A note[^1] [^1]: Big note. diff --git a/package.json b/package.json index b59e1c3..5212784 100644 --- a/package.json +++ b/package.json @@ -87,6 +87,7 @@ "remark-mdx-frontmatter": "^5.0.0", "solid-mdx": "^0.0.7", "unified": "^11.0.5", + "unist-builder": "^4.0.0", "unist-util-visit": "^5.0.0" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index db2a3e9..3b73933 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -104,6 +104,9 @@ importers: unified: specifier: ^11.0.5 version: 11.0.5 + unist-builder: + specifier: ^4.0.0 + version: 4.0.0 unist-util-visit: specifier: ^5.0.0 version: 5.0.0 @@ -3566,6 +3569,9 @@ packages: unimport@3.13.1: resolution: {integrity: sha512-nNrVzcs93yrZQOW77qnyOVHtb68LegvhYFwxFMfuuWScmwQmyVCG/NBuN8tYsaGzgQUVYv34E/af+Cc9u4og4A==} + unist-builder@4.0.0: + resolution: {integrity: sha512-wmRFnH+BLpZnTKpc5L7O67Kac89s9HMrtELpnNaE6TAobq5DTZZs5YaTQfAZBA9bFPECx2uVAPO31c+GVug8mg==} + unist-util-find-after@5.0.0: resolution: {integrity: sha512-amQa0Ep2m6hE2g72AugUItjbuM8X8cGQnFoHk0pGfrFeT9GZhzN5SW8nRsiGKK7Aif4CrACPENkA6P/Lw6fHGQ==} @@ -7914,6 +7920,10 @@ snapshots: - rollup - webpack-sources + unist-builder@4.0.0: + dependencies: + '@types/unist': 3.0.3 + unist-util-find-after@5.0.0: dependencies: '@types/unist': 3.0.3 diff --git a/src/config.ts b/src/config.ts index 5085c08..c79dfb4 100644 --- a/src/config.ts +++ b/src/config.ts @@ -23,6 +23,7 @@ import { rehypeFixExpressiveCodeJsx } from "./rehype-plugins"; import { remarkCustomContainers, remarkGithubAlertsToDirectives, + remarkIssueAutolink, remarkRelativeImports, remarkTOC, } from "./remark-plugins"; @@ -58,6 +59,7 @@ export type SolidBaseConfig = { titleTemplate?: string; componentsFolder?: string; editPath?: string | ((path: string) => string); + issueAutolink?: false | string | ((issue: string) => string); lastUpdated?: Intl.DateTimeFormatOptions | false; footer?: boolean; socialLinks?: @@ -75,7 +77,8 @@ type ResolvedConfigKeys = | "description" | "lastUpdated" | "footer" - | "lang"; + | "lang" + | "issueAutolink"; export type SolidBaseResolvedConfig = Omit< SolidBaseConfig, @@ -109,6 +112,7 @@ export function withSolidBase( baseConfig.footer ??= true; baseConfig.lang ??= "en-US"; baseConfig.sidebar ??= { items: [] }; + baseConfig.issueAutolink ??= false; process.env.PORT ??= "4000"; @@ -167,6 +171,7 @@ export function withSolidBase( remarkRelativeImports, remarkTOC, remarkCustomContainers, + [remarkIssueAutolink, baseConfig.issueAutolink], ], }), ); diff --git a/src/remark-plugins.ts b/src/remark-plugins.ts index eb773ac..2cf9a27 100644 --- a/src/remark-plugins.ts +++ b/src/remark-plugins.ts @@ -2,7 +2,9 @@ import { fromJs } from "esast-util-from-js"; import { h } from "hastscript"; import { findAndReplace } from "mdast-util-find-and-replace"; import { toc } from "mdast-util-toc"; +import { u } from "unist-builder"; import { visit } from "unist-util-visit"; +import { SolidBaseConfig, type SolidBaseResolvedConfig } from "./config"; interface ParagraphNode { type: "paragraph"; @@ -82,6 +84,35 @@ export function remarkTOC() { }; } +export function remarkIssueAutolink( + issueAutolink: SolidBaseResolvedConfig["issueAutolink"], +) { + if (issueAutolink === false) return; + + const url = (issue: string) => { + const number = issue.slice(1); + if (typeof issueAutolink === "function") return issueAutolink(number); + return issueAutolink.replace(":issue", number); + }; + + return (tree: any) => { + findAndReplace(tree, [ + [ + /(?<=(^| ))#\d+/gm, + (match: string) => { + return u("link", { url: url(match) }, [u("text", match)]); + }, + ], + [ + /\\#\d+/g, + (match: string) => { + return match.slice(1); + }, + ], + ]); + }; +} + const customContainers = new Set([ "info", "note",