Skip to content

Commit

Permalink
feat: issue autolink
Browse files Browse the repository at this point in the history
  • Loading branch information
jer3m01 committed Oct 24, 2024
1 parent 60f6228 commit 451808d
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions dev/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
2 changes: 2 additions & 0 deletions dev/src/routes/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ www.example.com

[Link internal /about](/about)

#20 also #4 but no \\#20

A note[^1]

[^1]: Big note.
Expand Down
1 change: 1 addition & 0 deletions docs/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
2 changes: 2 additions & 0 deletions docs/src/routes/guide/dev.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ www.example.com

[Link internal /about](/about)

#20 also #4 but no \\#20

A note[^1]

[^1]: Big note.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { rehypeFixExpressiveCodeJsx } from "./rehype-plugins";
import {
remarkCustomContainers,
remarkGithubAlertsToDirectives,
remarkIssueAutolink,
remarkRelativeImports,
remarkTOC,
} from "./remark-plugins";
Expand Down Expand Up @@ -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?:
Expand All @@ -75,7 +77,8 @@ type ResolvedConfigKeys =
| "description"
| "lastUpdated"
| "footer"
| "lang";
| "lang"
| "issueAutolink";

export type SolidBaseResolvedConfig = Omit<
SolidBaseConfig,
Expand Down Expand Up @@ -109,6 +112,7 @@ export function withSolidBase(
baseConfig.footer ??= true;
baseConfig.lang ??= "en-US";
baseConfig.sidebar ??= { items: [] };
baseConfig.issueAutolink ??= false;

process.env.PORT ??= "4000";

Expand Down Expand Up @@ -167,6 +171,7 @@ export function withSolidBase(
remarkRelativeImports,
remarkTOC,
remarkCustomContainers,
[remarkIssueAutolink, baseConfig.issueAutolink],
],
}),
);
Expand Down
31 changes: 31 additions & 0 deletions src/remark-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 451808d

Please sign in to comment.