Skip to content

Commit 3d5131d

Browse files
committed
環境変数を siteConfig 経由で渡す
1 parent 460f6d9 commit 3d5131d

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

.github/workflows/deploy.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ jobs:
1414
with:
1515
fetch-depth: 0
1616
- run: npm ci && npm run build
17-
env:
18-
COMMIT_REF: ${{ github.sha }}
1917
- uses: actions/upload-pages-artifact@v2
2018
with:
2119
path: build

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 & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +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";
6-
import { COMMIT_REF } from "../../env";
7+
8+
const repositoryRootPathLength =
9+
import.meta.url.split("/").slice(0, -4).join("/").length + 1;
710

811
/**
912
* @param {Object} props
13+
* @param {string} props.url
1014
* @param {boolean} props.path
1115
* @param {boolean} props.noCodeSandbox
1216
*/
1317
export default function ViewSource({ url, path, noCodeSandbox }) {
14-
const fullPathSplit = url.split("/");
15-
const docsIndex = fullPathSplit.indexOf("docs");
16-
const srcIndex = fullPathSplit.indexOf("src");
17-
const baseIndex = docsIndex !== -1 ? docsIndex : srcIndex;
18-
const pathFromBase = fullPathSplit.slice(baseIndex);
19-
const dirPath = pathFromBase.slice(0, pathFromBase.length - 1);
20-
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+
);
2128
return (
2229
<div className={styles.root}>
2330
<a
2431
className={clsx("button button--secondary", styles.button)}
2532
target="_blank"
2633
rel="noopener"
27-
href={`https://github.com/ut-code/utcode-learn/tree/${COMMIT_REF}/${relativePath}`}
34+
href={gitHubUrl.toString()}
2835
>
2936
<SiGithub className={styles.icon} />
3037
GitHub で表示
@@ -34,7 +41,7 @@ export default function ViewSource({ url, path, noCodeSandbox }) {
3441
className={clsx("button button--primary", styles.button)}
3542
target="_blank"
3643
rel="noopener"
37-
href={`https://githubbox.com/ut-code/utcode-learn/tree/${COMMIT_REF}/${relativePath}`}
44+
href={codeSandboxUrl.toString()}
3845
>
3946
このプログラムを実行する
4047
<BiLinkExternal className={styles.icon} />

src/env.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)