Skip to content

Commit 3aebc3f

Browse files
authored
Merge pull request #388 from ut-code/inject-commit-id
CodeSandbox へのリンクにコミットSHAを使用する
2 parents 330789a + 3d5131d commit 3aebc3f

2 files changed

Lines changed: 21 additions & 9 deletions

File tree

docusaurus.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { execSync } = require("node:child_process");
12
const math = require("remark-math");
23
const katex = require("rehype-katex");
34

@@ -18,6 +19,9 @@ module.exports = {
1819
markdown: {
1920
mermaid: true,
2021
},
22+
customFields: {
23+
commitRef: execSync("git rev-parse HEAD").toString().trim(),
24+
},
2125
themes: ["@docusaurus/theme-mermaid"],
2226
presets: [
2327
[

src/components/ViewSource/index.jsx

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,36 @@ import React from "react";
22
import clsx from "clsx";
33
import { BiLinkExternal } from "react-icons/bi";
44
import { SiGithub } from "react-icons/si";
5+
import useDocusaurusContext from "@docusaurus/useDocusaurusContext";
56
import styles from "./styles.module.css";
67

8+
const repositoryRootPathLength =
9+
import.meta.url.split("/").slice(0, -4).join("/").length + 1;
10+
711
/**
812
* @param {Object} props
13+
* @param {string} props.url
914
* @param {boolean} props.path
1015
* @param {boolean} props.noCodeSandbox
1116
*/
1217
export default function ViewSource({ url, path, noCodeSandbox }) {
13-
const fullPathSplit = url.split("/");
14-
const docsIndex = fullPathSplit.indexOf("docs");
15-
const srcIndex = fullPathSplit.indexOf("src");
16-
const baseIndex = docsIndex !== -1 ? docsIndex : srcIndex;
17-
const pathFromBase = fullPathSplit.slice(baseIndex);
18-
const dirPath = pathFromBase.slice(0, pathFromBase.length - 1);
19-
const relativePath = `${dirPath.join("/")}/${path}`;
18+
const basePath = url.slice(repositoryRootPathLength);
19+
const commitRef = useDocusaurusContext()?.siteConfig.customFields.commitRef;
20+
const gitHubUrl = new URL(
21+
path,
22+
`https://github.com/ut-code/utcode-learn/tree/${commitRef}/${basePath}`
23+
);
24+
const codeSandboxUrl = new URL(
25+
path,
26+
`https://githubbox.com/ut-code/utcode-learn/tree/${commitRef}/${basePath}`
27+
);
2028
return (
2129
<div className={styles.root}>
2230
<a
2331
className={clsx("button button--secondary", styles.button)}
2432
target="_blank"
2533
rel="noopener"
26-
href={`https://github.com/ut-code/utcode-learn/tree/master/${relativePath}`}
34+
href={gitHubUrl.toString()}
2735
>
2836
<SiGithub className={styles.icon} />
2937
GitHub で表示
@@ -33,7 +41,7 @@ export default function ViewSource({ url, path, noCodeSandbox }) {
3341
className={clsx("button button--primary", styles.button)}
3442
target="_blank"
3543
rel="noopener"
36-
href={`https://githubbox.com/ut-code/utcode-learn/tree/master/${relativePath}`}
44+
href={codeSandboxUrl.toString()}
3745
>
3846
このプログラムを実行する
3947
<BiLinkExternal className={styles.icon} />

0 commit comments

Comments
 (0)