@@ -2,29 +2,36 @@ import React from "react";
22import clsx from "clsx" ;
33import { BiLinkExternal } from "react-icons/bi" ;
44import { SiGithub } from "react-icons/si" ;
5+ import useDocusaurusContext from "@docusaurus/useDocusaurusContext" ;
56import 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 */
1317export 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 } />
0 commit comments