File tree Expand file tree Collapse file tree 3 files changed +39
-17
lines changed Expand file tree Collapse file tree 3 files changed +39
-17
lines changed Original file line number Diff line number Diff line change @@ -160,13 +160,21 @@ export function pathWithForwardSlashes(path: string) {
160160}
161161
162162export function ensureTrailingSlash ( path : string ) {
163- if ( ! path . endsWith ( "/" ) ) {
163+ if ( path && ! path . endsWith ( "/" ) ) {
164164 return path + "/" ;
165165 } else {
166166 return path ;
167167 }
168168}
169169
170+ export function removeTrailingSlash ( path : string ) {
171+ if ( path && path . endsWith ( "/" ) ) {
172+ return path . slice ( 0 , path . length - 1 ) ;
173+ } else {
174+ return path ;
175+ }
176+ }
177+
170178export function resolveGlobs (
171179 root : string ,
172180 globs : string [ ] ,
Original file line number Diff line number Diff line change @@ -62,6 +62,7 @@ import {
6262 repoUrlIcon ,
6363 websiteConfigActions ,
6464 websiteProjectConfig ,
65+ websiteRepoInfoFromUrl ,
6566} from "../website/website-config.ts" ;
6667
6768import { kSidebarLogo } from "../website/website-navigation.ts" ;
@@ -236,14 +237,21 @@ export async function bookProjectConfig(
236237
237238function siteRepoUrl ( site : Metadata ) {
238239 const repoUrl = site [ kSiteRepoUrl ] as string ;
240+ const branch = site [ kSiteRepoBranch ] || "main" ;
239241 if ( site [ kSiteRepoSubdir ] ) {
240242 const subdir = ensureTrailingSlash ( site [ kSiteRepoSubdir ] as string ) ;
241- const branch = site [ kSiteRepoBranch ] || "main" ;
242243 return pathWithForwardSlashes (
243244 join ( repoUrl , `tree/${ branch } /${ subdir } ` ) ,
244245 ) ;
245246 } else {
246- return repoUrl ;
247+ const repoInfo = websiteRepoInfoFromUrl ( repoUrl ) ;
248+ if ( repoInfo . path ) {
249+ return pathWithForwardSlashes (
250+ join ( repoInfo . baseUrl , `tree/${ branch } ` , repoInfo . path ) ,
251+ ) ;
252+ } else {
253+ return repoInfo . baseUrl ;
254+ }
247255 }
248256}
249257
Original file line number Diff line number Diff line change @@ -37,7 +37,10 @@ import {
3737 kSiteUrl ,
3838 kWebsite ,
3939} from "./website-constants.ts" ;
40- import { ensureTrailingSlash } from "../../../core/path.ts" ;
40+ import {
41+ ensureTrailingSlash ,
42+ removeTrailingSlash ,
43+ } from "../../../core/path.ts" ;
4144type WebsiteConfigKey =
4245 | "title"
4346 | "image"
@@ -194,25 +197,28 @@ export function websiteRepoInfo(
194197 path : ensureTrailingSlash ( repoSubdir ) ,
195198 } ;
196199 } else {
197- // extract into base and path
198- const match = repoUrl . match ( / ( h t t p s ? : \/ \/ (?: [ ^ \/ ] + \/ ) { 3 } ) ( .* ) / ) ;
199- if ( match ) {
200- return {
201- baseUrl : match [ 1 ] ,
202- path : ensureTrailingSlash ( match [ 2 ] ) || "" ,
203- } ;
204- } else {
205- return {
206- baseUrl : repoUrl ,
207- path : "" ,
208- } ;
209- }
200+ return websiteRepoInfoFromUrl ( repoUrl ) ;
210201 }
211202 } else {
212203 return undefined ;
213204 }
214205}
215206
207+ export function websiteRepoInfoFromUrl ( repoUrl : string ) {
208+ const match = repoUrl . match ( / ( h t t p s ? : \/ \/ (?: [ ^ \/ ] + \/ ) { 3 } ) ( .* ) / ) ;
209+ if ( match ) {
210+ return {
211+ baseUrl : match [ 1 ] ,
212+ path : ensureTrailingSlash ( match [ 2 ] ) || "" ,
213+ } ;
214+ } else {
215+ return {
216+ baseUrl : repoUrl ,
217+ path : "" ,
218+ } ;
219+ }
220+ }
221+
216222export function websiteRepoBranch ( project ?: ProjectConfig ) : string {
217223 return websiteConfigString ( kSiteRepoBranch , project ) || "main" ;
218224}
You can’t perform that action at this time.
0 commit comments