@@ -2,28 +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" ;
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 */
1217export 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