Skip to content

Commit 072ff8f

Browse files
authored
Merge pull request #30 from yacchin1205/feature/base-path
Support for subdirectories
2 parents ae8fd6c + c0732d0 commit 072ff8f

File tree

8 files changed

+63
-18
lines changed

8 files changed

+63
-18
lines changed

Dockerfile

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1+
ARG ETHERPAD_IMAGE_NAME="etherpad/etherpad"
2+
ARG ETHERPAD_IMAGE_TAG="2"
3+
14
FROM mcr.microsoft.com/devcontainers/typescript-node:18 AS build-stage
25

36
COPY . /app/ep_weave
47
RUN cd /app/ep_weave \
58
&& ls -la /app/ep_weave \
69
&& npm i --include dev && npm run build
710

8-
FROM etherpad/etherpad:2
11+
FROM ${ETHERPAD_IMAGE_NAME}:${ETHERPAD_IMAGE_TAG}
912

1013
USER root
1114

1215
COPY --from=build-stage /app/ep_weave /tmp/ep_weave
1316

1417
# ep_search
15-
RUN git clone -b feature/search-engine-ep2 https://github.com/yacchin1205/ep_search.git /tmp/ep_search \
18+
RUN git clone -b feature/search-engine https://github.com/NII-cloud-operation/ep_search.git /tmp/ep_search \
1619
&& cd /tmp/ep_search \
1720
&& ls -la /tmp/ep_search \
1821
&& npm pack

src/pad/index.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ const api = require("ep_etherpad-lite/node/db/API");
1717

1818
let apikey: string | null = null;
1919

20+
type PluginSettings = {
21+
basePath?: string;
22+
};
23+
2024
async function getPadIdsByTitle(searchEngine: SearchEngine, title: string) {
2125
const results = await searchEngine.search(
2226
`title:"${escapeForText(title)}"`
@@ -81,6 +85,8 @@ exports.registerRoute = (
8185
args: ExpressCreateServerArgs,
8286
cb: (next: any) => void
8387
) => {
88+
const epWeavePluginSettings = (settings.ep_weave || {}) as PluginSettings;
89+
const basePath = epWeavePluginSettings.basePath || "";
8490
const pluginSettings = settings.ep_search || {};
8591
const searchEngine = createSearchEngine(pluginSettings);
8692
const apikeyFilename = absolutePaths.makeAbsolute(
@@ -141,7 +147,7 @@ exports.registerRoute = (
141147
if (ids === null) {
142148
createNewPadForTitle(title, req)
143149
.then((id) => {
144-
res.redirect(`/p/${id}`);
150+
res.redirect(`${basePath}/p/${id}`);
145151
})
146152
.catch((err) => {
147153
console.error(
@@ -155,7 +161,7 @@ exports.registerRoute = (
155161
});
156162
return;
157163
}
158-
res.redirect(`/p/${ids[0]}`);
164+
res.redirect(`${basePath}/p/${ids[0]}`);
159165
})
160166
.catch((err) => {
161167
console.error(

src/pad/static/js/hashitem.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { PadType } from "ep_search/setup";
22
import { getColorFromTitle, contrastRatio } from "./color";
3+
import { getBasePath } from "./util";
34

45
function mostReadableColor(backgroundColor: string, colorCandidates: string[]) {
56
const contrastRatios = colorCandidates.map((color) =>
@@ -25,9 +26,9 @@ export async function createHashItemView(doc: PadType) {
2526
"#cccccc",
2627
"#ffffff",
2728
]);
28-
29+
const basePath = getBasePath();
2930
const anchor = $("<a></a>")
30-
.attr("href", `/p/${value}`)
31+
.attr("href", `${basePath}/p/${value}`)
3132
.css("color", color)
3233
.text(title);
3334
const hashLink = $("<div></div>")

src/pad/static/js/hashview.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { query, escapeForText } from "./result";
1212
import { createToolbar, createCloseButton } from "./toolbar";
1313
import { initResizer, windowResized } from "./resizer";
1414
import { getHashQuery } from "./hash";
15+
import { getBasePath } from "./util";
1516

1617
type PadRef = {
1718
id: string;
@@ -47,7 +48,8 @@ function getPadURL() {
4748
const url = new URL(window.location.href);
4849
url.search = "";
4950
url.hash = "";
50-
url.pathname = `/t/${encodeURIComponent(ep_weave.title)}`;
51+
const basePath = getBasePath();
52+
url.pathname = `${basePath}/t/${encodeURIComponent(ep_weave.title)}`;
5153
return url.toString();
5254
}
5355

@@ -83,10 +85,11 @@ function overrideEmbedCommand(toolbar: AceToolbar) {
8385
}
8486

8587
function refreshNavbar(navbar: JQuery, title: string) {
88+
const basePath = getBasePath();
8689
navbar.empty();
8790
navbar.append(
8891
$("<a>")
89-
.attr("href", "/")
92+
.attr("href", `${basePath}/`)
9093
.text("Index")
9194
.addClass("hashview-path-segment hashview-path-index")
9295
);
@@ -99,7 +102,7 @@ function refreshNavbar(navbar: JQuery, title: string) {
99102
$("<a>")
100103
.addClass("hashview-path-segment")
101104
.text(segment)
102-
.attr("href", `/t/${encodeURIComponent(parentPath)}`)
105+
.attr("href", `${basePath}/t/${encodeURIComponent(parentPath)}`)
103106
);
104107
navbar.append($("<span>").addClass("hashview-path-separator").text("/"));
105108
}
@@ -129,14 +132,15 @@ function getCurrentSort() {
129132
}
130133

131134
function createMenuItem() {
135+
const basePath = getBasePath();
132136
const changeTitleButton = $("<button></button>")
133137
.addClass("hashview-change-title btn")
134138
.on("click", () => {
135139
if (!changedTitle) {
136140
return;
137141
}
138142
$.ajax({
139-
url: `/ep_weave/hashes?${new URLSearchParams({
143+
url: `${basePath}/ep_weave/hashes?${new URLSearchParams({
140144
oldtitle: changedTitle.oldtitle,
141145
newtitle: changedTitle.newtitle,
142146
})}`,
@@ -175,8 +179,9 @@ function createMenuItem() {
175179
return;
176180
}
177181
duplicatedPads.forEach((pad) => {
182+
const basePath = getBasePath();
178183
console.debug(logPrefix, "Open pad", pad);
179-
window.open(`/p/${pad.id}`, "_blank");
184+
window.open(`${basePath}/p/${pad.id}`, "_blank");
180185
});
181186
});
182187
return $("<li></li>")
@@ -332,10 +337,11 @@ async function loadHashView(
332337
(await Promise.all(hashViews)).forEach((hashView) => {
333338
container.append(hashView);
334339
});
340+
const basePath = getBasePath();
335341
const titledPadExists = docs.some((doc) => doc.title === hash.substring(1));
336342
if (title !== hash.substring(1) && !titledPadExists) {
337343
const anchor = $("<a></a>")
338-
.attr("href", `/t/${hash.substring(1)}`)
344+
.attr("href", `${basePath}/t/${hash.substring(1)}`)
339345
.text(hash.substring(1));
340346
const createClass = "hash-create";
341347
const hashLink = $("<div></div>")
@@ -505,7 +511,8 @@ exports.postToolbarInit = (hook: any, context: PostToolbarInit) => {
505511
title += "/";
506512
}
507513
title += query;
508-
window.open(`/t/${encodeURIComponent(title)}`, "_blank");
514+
const basePath = getBasePath();
515+
window.open(`${basePath}/t/${encodeURIComponent(title)}`, "_blank");
509516
},
510517
}).prepend($("<div>").text(">").addClass("hashview-toolbar-child-marker"))
511518
)
@@ -577,9 +584,10 @@ export function aceCreateDomLine(
577584
if (!hashTitle) {
578585
throw new Error(`Unexpected error: ${searchHash_}, ${hash}, ${link}`);
579586
}
587+
const basePath = getBasePath();
580588
return [
581589
{
582-
extraOpenTags: `<a href="/t/${encodeURIComponent(hashTitle)}">`,
590+
extraOpenTags: `<a href="${basePath}/t/${encodeURIComponent(hashTitle)}">`,
583591
extraCloseTags: "</a>",
584592
cls: modifiedCls,
585593
},

src/pad/static/js/result.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { SearchResponse } from "ep_search/setup";
2+
import { getBasePath } from "./util";
23

34
export function escapeForText(query: string): string {
45
let escaped = query;
@@ -34,8 +35,9 @@ export function query(
3435
if (rows !== undefined) {
3536
opts.push(`&rows=${rows}`);
3637
}
38+
const basePath = getBasePath();
3739
$.getJSON(
38-
`/search/?query=${encodeURIComponent(query)}${opts.join("")}`,
40+
`${basePath}/search/?query=${encodeURIComponent(query)}${opts.join("")}`,
3941
(data: SearchResponse) => {
4042
resolve(data);
4143
}

src/pad/static/js/toolbar.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { getBasePath } from "./util";
2+
13
export type Callbacks = {
24
onSearch: (query: string) => void;
35
onSort: (sort: string) => void;
@@ -82,8 +84,9 @@ function createSearchBox(
8284
if (typeof query !== "string") {
8385
return;
8486
}
87+
const basePath = getBasePath();
8588
if (!onCreate) {
86-
window.open(`/t/${encodeURIComponent(query)}`, "_blank");
89+
window.open(`${basePath}/t/${encodeURIComponent(query)}`, "_blank");
8790
return;
8891
}
8992
onCreate(query);

src/pad/static/js/util.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export function getBasePath() {
2+
// The path is in the form of .../p/id or .../t/title, so get the part before that
3+
const path = window.location.pathname;
4+
let index = path.indexOf("/p/");
5+
if (index < 0) {
6+
index = path.indexOf("/t/");
7+
if (index < 0) {
8+
console.warn("Base path not found", path);
9+
// remove the last part of the path
10+
const lastSlash = path.lastIndexOf("/");
11+
if (lastSlash >= 0) {
12+
return path.substring(0, lastSlash);
13+
} else {
14+
return "";
15+
}
16+
}
17+
}
18+
return path.substring(0, index);
19+
}

src/pad/templates/index.html

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<link rel="stylesheet" href="../static/plugins/ep_weave/static/css/index.css" type="text/css" />
2-
<link rel="stylesheet" href="../static/plugins/ep_weave/static/css/styles.css" type="text/css" />
1+
<link rel="stylesheet" href="./static/plugins/ep_weave/static/css/index.css" type="text/css" />
2+
<link rel="stylesheet" href="./static/plugins/ep_weave/static/css/styles.css" type="text/css" />
33

44
<div id="weave-index">
55
<div class="weave-toolbar">
@@ -16,6 +16,9 @@
1616

1717
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js">
1818
</script>
19+
<script src="https://code.jquery.com/jquery-3.7.1.slim.min.js"
20+
integrity="sha256-kmHvs0B+OpCW5GVHUNjv9rOmY0IvSIRcf7zGUDTDQM8="
21+
crossorigin="anonymous"></script>
1922

2023
<script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous">
2124
</script>

0 commit comments

Comments
 (0)