From 460f6d958cd727be09a98ab25d3cff071de8e784 Mon Sep 17 00:00:00 2001 From: chelproc Date: Tue, 26 Sep 2023 10:04:37 +0900 Subject: [PATCH 1/2] =?UTF-8?q?CodeSandbox=20=E3=81=B8=E3=81=AE=E3=83=AA?= =?UTF-8?q?=E3=83=B3=E3=82=AF=E3=81=AB=E3=82=B3=E3=83=9F=E3=83=83=E3=83=88?= =?UTF-8?q?SHA=E3=82=92=E4=BD=BF=E7=94=A8=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy.yml | 2 ++ src/components/ViewSource/index.jsx | 5 +++-- src/env.js | 1 + 3 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 src/env.js diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index d42417508..8fc4f3932 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -14,6 +14,8 @@ jobs: with: fetch-depth: 0 - run: npm ci && npm run build + env: + COMMIT_REF: ${{ github.sha }} - uses: actions/upload-pages-artifact@v2 with: path: build diff --git a/src/components/ViewSource/index.jsx b/src/components/ViewSource/index.jsx index 37689d492..9b9a84a76 100644 --- a/src/components/ViewSource/index.jsx +++ b/src/components/ViewSource/index.jsx @@ -3,6 +3,7 @@ import clsx from "clsx"; import { BiLinkExternal } from "react-icons/bi"; import { SiGithub } from "react-icons/si"; import styles from "./styles.module.css"; +import { COMMIT_REF } from "../../env"; /** * @param {Object} props @@ -23,7 +24,7 @@ export default function ViewSource({ url, path, noCodeSandbox }) { className={clsx("button button--secondary", styles.button)} target="_blank" rel="noopener" - href={`https://github.com/ut-code/utcode-learn/tree/master/${relativePath}`} + href={`https://github.com/ut-code/utcode-learn/tree/${COMMIT_REF}/${relativePath}`} > GitHub で表示 @@ -33,7 +34,7 @@ export default function ViewSource({ url, path, noCodeSandbox }) { className={clsx("button button--primary", styles.button)} target="_blank" rel="noopener" - href={`https://githubbox.com/ut-code/utcode-learn/tree/master/${relativePath}`} + href={`https://githubbox.com/ut-code/utcode-learn/tree/${COMMIT_REF}/${relativePath}`} > このプログラムを実行する diff --git a/src/env.js b/src/env.js new file mode 100644 index 000000000..9ede9f14f --- /dev/null +++ b/src/env.js @@ -0,0 +1 @@ +export const COMMIT_REF = process.env.COMMIT_REF ?? "HEAD"; From 3d5131d33c8412cd683b727243b5b5a43f42c37e Mon Sep 17 00:00:00 2001 From: chelproc Date: Tue, 26 Sep 2023 11:24:11 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E7=92=B0=E5=A2=83=E5=A4=89=E6=95=B0?= =?UTF-8?q?=E3=82=92=20siteConfig=20=E7=B5=8C=E7=94=B1=E3=81=A7=E6=B8=A1?= =?UTF-8?q?=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy.yml | 2 -- docusaurus.config.js | 4 ++++ src/components/ViewSource/index.jsx | 27 +++++++++++++++++---------- src/env.js | 1 - 4 files changed, 21 insertions(+), 13 deletions(-) delete mode 100644 src/env.js diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 8fc4f3932..d42417508 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -14,8 +14,6 @@ jobs: with: fetch-depth: 0 - run: npm ci && npm run build - env: - COMMIT_REF: ${{ github.sha }} - uses: actions/upload-pages-artifact@v2 with: path: build diff --git a/docusaurus.config.js b/docusaurus.config.js index e15cf2b65..d7a8cb855 100644 --- a/docusaurus.config.js +++ b/docusaurus.config.js @@ -1,3 +1,4 @@ +const { execSync } = require("node:child_process"); const math = require("remark-math"); const katex = require("rehype-katex"); @@ -18,6 +19,9 @@ module.exports = { markdown: { mermaid: true, }, + customFields: { + commitRef: execSync("git rev-parse HEAD").toString().trim(), + }, themes: ["@docusaurus/theme-mermaid"], presets: [ [ diff --git a/src/components/ViewSource/index.jsx b/src/components/ViewSource/index.jsx index 9b9a84a76..8fa96f3f2 100644 --- a/src/components/ViewSource/index.jsx +++ b/src/components/ViewSource/index.jsx @@ -2,29 +2,36 @@ import React from "react"; import clsx from "clsx"; import { BiLinkExternal } from "react-icons/bi"; import { SiGithub } from "react-icons/si"; +import useDocusaurusContext from "@docusaurus/useDocusaurusContext"; import styles from "./styles.module.css"; -import { COMMIT_REF } from "../../env"; + +const repositoryRootPathLength = + import.meta.url.split("/").slice(0, -4).join("/").length + 1; /** * @param {Object} props + * @param {string} props.url * @param {boolean} props.path * @param {boolean} props.noCodeSandbox */ export default function ViewSource({ url, path, noCodeSandbox }) { - const fullPathSplit = url.split("/"); - const docsIndex = fullPathSplit.indexOf("docs"); - const srcIndex = fullPathSplit.indexOf("src"); - const baseIndex = docsIndex !== -1 ? docsIndex : srcIndex; - const pathFromBase = fullPathSplit.slice(baseIndex); - const dirPath = pathFromBase.slice(0, pathFromBase.length - 1); - const relativePath = `${dirPath.join("/")}/${path}`; + const basePath = url.slice(repositoryRootPathLength); + const commitRef = useDocusaurusContext()?.siteConfig.customFields.commitRef; + const gitHubUrl = new URL( + path, + `https://github.com/ut-code/utcode-learn/tree/${commitRef}/${basePath}` + ); + const codeSandboxUrl = new URL( + path, + `https://githubbox.com/ut-code/utcode-learn/tree/${commitRef}/${basePath}` + ); return (
GitHub で表示 @@ -34,7 +41,7 @@ export default function ViewSource({ url, path, noCodeSandbox }) { className={clsx("button button--primary", styles.button)} target="_blank" rel="noopener" - href={`https://githubbox.com/ut-code/utcode-learn/tree/${COMMIT_REF}/${relativePath}`} + href={codeSandboxUrl.toString()} > このプログラムを実行する diff --git a/src/env.js b/src/env.js deleted file mode 100644 index 9ede9f14f..000000000 --- a/src/env.js +++ /dev/null @@ -1 +0,0 @@ -export const COMMIT_REF = process.env.COMMIT_REF ?? "HEAD";