Skip to content

Commit 9d33db3

Browse files
committed
repo-subdir option
1 parent 0217484 commit 9d33db3

File tree

8 files changed

+79
-14
lines changed

8 files changed

+79
-14
lines changed

src/core/path.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ export function pathWithForwardSlashes(path: string) {
159159
return path.replace(/\\/g, "/");
160160
}
161161

162+
export function ensureTrailingSlash(path: string) {
163+
if (!path.endsWith("/")) {
164+
return path + "/";
165+
} else {
166+
return path;
167+
}
168+
}
169+
162170
export function resolveGlobs(
163171
root: string,
164172
globs: string[],

src/project/types/book/book-config.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import { basename, join } from "path/mod.ts";
1010

1111
import * as ld from "../../../core/lodash.ts";
1212

13-
import { safeExistsSync } from "../../../core/path.ts";
13+
import {
14+
ensureTrailingSlash,
15+
pathWithForwardSlashes,
16+
safeExistsSync,
17+
} from "../../../core/path.ts";
1418
import { FormatLanguage, Metadata } from "../../../config/types.ts";
1519

1620
import {
@@ -44,6 +48,7 @@ import {
4448
kSiteReaderMode,
4549
kSiteRepoActions,
4650
kSiteRepoBranch,
51+
kSiteRepoSubdir,
4752
kSiteRepoUrl,
4853
kSiteSidebar,
4954
kSiteSidebarStyle,
@@ -118,6 +123,7 @@ export async function bookProjectConfig(
118123
site[kSiteUrl] = book[kSiteUrl];
119124
site[kSitePath] = book[kSitePath];
120125
site[kSiteRepoUrl] = book[kSiteRepoUrl];
126+
site[kSiteRepoSubdir] = book[kSiteRepoSubdir];
121127
site[kSiteRepoBranch] = book[kSiteRepoBranch];
122128
site[kSiteRepoActions] = book[kSiteRepoActions];
123129
site[kSiteNavbar] = book[kSiteNavbar];
@@ -190,7 +196,7 @@ export async function bookProjectConfig(
190196
// code tools
191197
const tools = [];
192198
if (site[kSiteRepoUrl]) {
193-
const repoUrl = site[kSiteRepoUrl] as string;
199+
const repoUrl = siteRepoUrl(site);
194200
const icon = repoUrlIcon(repoUrl);
195201
tools.push({
196202
text: "Source Code",
@@ -228,6 +234,19 @@ export async function bookProjectConfig(
228234
return websiteProjectConfig(projectDir, config, forceHtml);
229235
}
230236

237+
function siteRepoUrl(site: Metadata) {
238+
const repoUrl = site[kSiteRepoUrl] as string;
239+
if (site[kSiteRepoSubdir]) {
240+
const subdir = ensureTrailingSlash(site[kSiteRepoSubdir] as string);
241+
const branch = site[kSiteRepoBranch] || "main";
242+
return pathWithForwardSlashes(
243+
join(repoUrl, `tree/${branch}/${subdir}`),
244+
);
245+
} else {
246+
return repoUrl;
247+
}
248+
}
249+
231250
const variableRegex = /{{<\s*var\s+(.*?)\s*>}}/gm;
232251
function resolveVariables(value: string, config: ProjectConfig) {
233252
variableRegex.lastIndex = 0;

src/project/types/website/website-config.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
kMarginHeader,
2929
kSitePath,
3030
kSiteRepoBranch,
31+
kSiteRepoSubdir,
3132
kSiteRepoUrl,
3233
kSiteSidebar,
3334
kSiteSidebarFooter,
@@ -36,6 +37,7 @@ import {
3637
kSiteUrl,
3738
kWebsite,
3839
} from "./website-constants.ts";
40+
import { ensureTrailingSlash } from "../../../core/path.ts";
3941
type WebsiteConfigKey =
4042
| "title"
4143
| "image"
@@ -44,6 +46,7 @@ type WebsiteConfigKey =
4446
| "site-url"
4547
| "site-path"
4648
| "repo-url"
49+
| "repo-subdir"
4750
| "repo-branch"
4851
| "repo-actions"
4952
| "navbar"
@@ -182,21 +185,28 @@ export function websiteRepoInfo(
182185
): WebsiteRepoInfo | undefined {
183186
let repoUrl = websiteConfigString(kSiteRepoUrl, project);
184187
if (repoUrl) {
185-
if (!repoUrl.endsWith("/")) {
186-
repoUrl = repoUrl + "/";
187-
}
188-
// extract into base and path
189-
const match = repoUrl.match(/(https?:\/\/(?:[^\/]+\/){3})(.*)/);
190-
if (match) {
191-
return {
192-
baseUrl: match[1],
193-
path: match[2] || "",
194-
};
195-
} else {
188+
repoUrl = ensureTrailingSlash(repoUrl);
189+
// is there an explicit subdir?
190+
const repoSubdir = websiteConfigString(kSiteRepoSubdir, project);
191+
if ((repoSubdir)) {
196192
return {
197193
baseUrl: repoUrl,
198-
path: "",
194+
path: ensureTrailingSlash(repoSubdir),
199195
};
196+
} else {
197+
// extract into base and path
198+
const match = repoUrl.match(/(https?:\/\/(?:[^\/]+\/){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+
}
200210
}
201211
} else {
202212
return undefined;

src/project/types/website/website-constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const kSitePath = "site-path";
1212
export const kSiteTitle = "title";
1313
export const kSiteFavicon = "favicon";
1414
export const kSiteRepoUrl = "repo-url";
15+
export const kSiteRepoSubdir = "repo-subdir";
1516
export const kSiteRepoBranch = "repo-branch";
1617
export const kSiteRepoActions = "repo-actions";
1718
export const kSiteReaderMode = "reader-mode";

src/resources/editor/tools/vs-code.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8858,6 +8858,11 @@ var require_yaml_intelligence_resources = __commonJS({
88588858
description: "Base URL for website source code repository"
88598859
}
88608860
},
8861+
"repo-subdir": {
8862+
string: {
8863+
description: "Subdirectory of repository containing website"
8864+
}
8865+
},
88618866
"repo-branch": {
88628867
string: {
88638868
description: "Branch of website source code (defaults to `main`)"
@@ -16907,6 +16912,7 @@ var require_yaml_intelligence_resources = __commonJS({
1690716912
"Base URL for published website",
1690816913
"Path to site (defaults to \u2018/\u2019). Not required if you specify\n<code>site-url</code>.",
1690916914
"Base URL for website source code repository",
16915+
"Subdirectory of repository containing website or book",
1691016916
"Branch of website source code (defaults to <code>main</code>)",
1691116917
{
1691216918
short: "Links to source repository actions",
@@ -18390,6 +18396,7 @@ var require_yaml_intelligence_resources = __commonJS({
1839018396
"Base URL for published website",
1839118397
"Path to site (defaults to \u2018/\u2019). Not required if you specify\n<code>site-url</code>.",
1839218398
"Base URL for website source code repository",
18399+
"Subdirectory of repository containing website or book",
1839318400
"Branch of website source code (defaults to <code>main</code>)",
1839418401
{
1839518402
short: "Links to source repository actions",
@@ -18589,6 +18596,7 @@ var require_yaml_intelligence_resources = __commonJS({
1858918596
"Base URL for published website",
1859018597
"Path to site (defaults to \u2018/\u2019). Not required if you specify\n<code>site-url</code>.",
1859118598
"Base URL for website source code repository",
18599+
"Subdirectory of repository containing website or book",
1859218600
"Branch of website source code (defaults to <code>main</code>)",
1859318601
{
1859418602
short: "Links to source repository actions",

src/resources/editor/tools/yaml/web-worker.js

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/resources/editor/tools/yaml/yaml-intelligence-resources.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,6 +1837,11 @@
18371837
"description": "Base URL for website source code repository"
18381838
}
18391839
},
1840+
"repo-subdir": {
1841+
"string": {
1842+
"description": "Subdirectory of repository containing website"
1843+
}
1844+
},
18401845
"repo-branch": {
18411846
"string": {
18421847
"description": "Branch of website source code (defaults to `main`)"
@@ -9886,6 +9891,7 @@
98869891
"Base URL for published website",
98879892
"Path to site (defaults to ‘/’). Not required if you specify\n<code>site-url</code>.",
98889893
"Base URL for website source code repository",
9894+
"Subdirectory of repository containing website or book",
98899895
"Branch of website source code (defaults to <code>main</code>)",
98909896
{
98919897
"short": "Links to source repository actions",
@@ -11369,6 +11375,7 @@
1136911375
"Base URL for published website",
1137011376
"Path to site (defaults to ‘/’). Not required if you specify\n<code>site-url</code>.",
1137111377
"Base URL for website source code repository",
11378+
"Subdirectory of repository containing website or book",
1137211379
"Branch of website source code (defaults to <code>main</code>)",
1137311380
{
1137411381
"short": "Links to source repository actions",
@@ -11568,6 +11575,7 @@
1156811575
"Base URL for published website",
1156911576
"Path to site (defaults to ‘/’). Not required if you specify\n<code>site-url</code>.",
1157011577
"Base URL for website source code repository",
11578+
"Subdirectory of repository containing website or book",
1157111579
"Branch of website source code (defaults to <code>main</code>)",
1157211580
{
1157311581
"short": "Links to source repository actions",

src/resources/schema/definitions.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,9 @@
485485
repo-url:
486486
string:
487487
description: "Base URL for website source code repository"
488+
repo-subdir:
489+
string:
490+
description: "Subdirectory of repository containing website"
488491
repo-branch:
489492
string:
490493
description: "Branch of website source code (defaults to `main`)"

0 commit comments

Comments
 (0)