Skip to content

Commit

Permalink
Merge pull request #30 from yacchin1205/feature/base-path
Browse files Browse the repository at this point in the history
Support for subdirectories
  • Loading branch information
yacchin1205 authored Sep 3, 2024
2 parents ae8fd6c + c0732d0 commit 072ff8f
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 18 deletions.
7 changes: 5 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
ARG ETHERPAD_IMAGE_NAME="etherpad/etherpad"
ARG ETHERPAD_IMAGE_TAG="2"

FROM mcr.microsoft.com/devcontainers/typescript-node:18 AS build-stage

COPY . /app/ep_weave
RUN cd /app/ep_weave \
&& ls -la /app/ep_weave \
&& npm i --include dev && npm run build

FROM etherpad/etherpad:2
FROM ${ETHERPAD_IMAGE_NAME}:${ETHERPAD_IMAGE_TAG}

USER root

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

# ep_search
RUN git clone -b feature/search-engine-ep2 https://github.com/yacchin1205/ep_search.git /tmp/ep_search \
RUN git clone -b feature/search-engine https://github.com/NII-cloud-operation/ep_search.git /tmp/ep_search \
&& cd /tmp/ep_search \
&& ls -la /tmp/ep_search \
&& npm pack
Expand Down
10 changes: 8 additions & 2 deletions src/pad/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const api = require("ep_etherpad-lite/node/db/API");

let apikey: string | null = null;

type PluginSettings = {
basePath?: string;
};

async function getPadIdsByTitle(searchEngine: SearchEngine, title: string) {
const results = await searchEngine.search(
`title:"${escapeForText(title)}"`
Expand Down Expand Up @@ -81,6 +85,8 @@ exports.registerRoute = (
args: ExpressCreateServerArgs,
cb: (next: any) => void
) => {
const epWeavePluginSettings = (settings.ep_weave || {}) as PluginSettings;
const basePath = epWeavePluginSettings.basePath || "";
const pluginSettings = settings.ep_search || {};
const searchEngine = createSearchEngine(pluginSettings);
const apikeyFilename = absolutePaths.makeAbsolute(
Expand Down Expand Up @@ -141,7 +147,7 @@ exports.registerRoute = (
if (ids === null) {
createNewPadForTitle(title, req)
.then((id) => {
res.redirect(`/p/${id}`);
res.redirect(`${basePath}/p/${id}`);
})
.catch((err) => {
console.error(
Expand All @@ -155,7 +161,7 @@ exports.registerRoute = (
});
return;
}
res.redirect(`/p/${ids[0]}`);
res.redirect(`${basePath}/p/${ids[0]}`);
})
.catch((err) => {
console.error(
Expand Down
5 changes: 3 additions & 2 deletions src/pad/static/js/hashitem.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PadType } from "ep_search/setup";
import { getColorFromTitle, contrastRatio } from "./color";
import { getBasePath } from "./util";

function mostReadableColor(backgroundColor: string, colorCandidates: string[]) {
const contrastRatios = colorCandidates.map((color) =>
Expand All @@ -25,9 +26,9 @@ export async function createHashItemView(doc: PadType) {
"#cccccc",
"#ffffff",
]);

const basePath = getBasePath();
const anchor = $("<a></a>")
.attr("href", `/p/${value}`)
.attr("href", `${basePath}/p/${value}`)
.css("color", color)
.text(title);
const hashLink = $("<div></div>")
Expand Down
24 changes: 16 additions & 8 deletions src/pad/static/js/hashview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { query, escapeForText } from "./result";
import { createToolbar, createCloseButton } from "./toolbar";
import { initResizer, windowResized } from "./resizer";
import { getHashQuery } from "./hash";
import { getBasePath } from "./util";

type PadRef = {
id: string;
Expand Down Expand Up @@ -47,7 +48,8 @@ function getPadURL() {
const url = new URL(window.location.href);
url.search = "";
url.hash = "";
url.pathname = `/t/${encodeURIComponent(ep_weave.title)}`;
const basePath = getBasePath();
url.pathname = `${basePath}/t/${encodeURIComponent(ep_weave.title)}`;
return url.toString();
}

Expand Down Expand Up @@ -83,10 +85,11 @@ function overrideEmbedCommand(toolbar: AceToolbar) {
}

function refreshNavbar(navbar: JQuery, title: string) {
const basePath = getBasePath();
navbar.empty();
navbar.append(
$("<a>")
.attr("href", "/")
.attr("href", `${basePath}/`)
.text("Index")
.addClass("hashview-path-segment hashview-path-index")
);
Expand All @@ -99,7 +102,7 @@ function refreshNavbar(navbar: JQuery, title: string) {
$("<a>")
.addClass("hashview-path-segment")
.text(segment)
.attr("href", `/t/${encodeURIComponent(parentPath)}`)
.attr("href", `${basePath}/t/${encodeURIComponent(parentPath)}`)
);
navbar.append($("<span>").addClass("hashview-path-separator").text("/"));
}
Expand Down Expand Up @@ -129,14 +132,15 @@ function getCurrentSort() {
}

function createMenuItem() {
const basePath = getBasePath();
const changeTitleButton = $("<button></button>")
.addClass("hashview-change-title btn")
.on("click", () => {
if (!changedTitle) {
return;
}
$.ajax({
url: `/ep_weave/hashes?${new URLSearchParams({
url: `${basePath}/ep_weave/hashes?${new URLSearchParams({
oldtitle: changedTitle.oldtitle,
newtitle: changedTitle.newtitle,
})}`,
Expand Down Expand Up @@ -175,8 +179,9 @@ function createMenuItem() {
return;
}
duplicatedPads.forEach((pad) => {
const basePath = getBasePath();
console.debug(logPrefix, "Open pad", pad);
window.open(`/p/${pad.id}`, "_blank");
window.open(`${basePath}/p/${pad.id}`, "_blank");
});
});
return $("<li></li>")
Expand Down Expand Up @@ -332,10 +337,11 @@ async function loadHashView(
(await Promise.all(hashViews)).forEach((hashView) => {
container.append(hashView);
});
const basePath = getBasePath();
const titledPadExists = docs.some((doc) => doc.title === hash.substring(1));
if (title !== hash.substring(1) && !titledPadExists) {
const anchor = $("<a></a>")
.attr("href", `/t/${hash.substring(1)}`)
.attr("href", `${basePath}/t/${hash.substring(1)}`)
.text(hash.substring(1));
const createClass = "hash-create";
const hashLink = $("<div></div>")
Expand Down Expand Up @@ -505,7 +511,8 @@ exports.postToolbarInit = (hook: any, context: PostToolbarInit) => {
title += "/";
}
title += query;
window.open(`/t/${encodeURIComponent(title)}`, "_blank");
const basePath = getBasePath();
window.open(`${basePath}/t/${encodeURIComponent(title)}`, "_blank");
},
}).prepend($("<div>").text(">").addClass("hashview-toolbar-child-marker"))
)
Expand Down Expand Up @@ -577,9 +584,10 @@ export function aceCreateDomLine(
if (!hashTitle) {
throw new Error(`Unexpected error: ${searchHash_}, ${hash}, ${link}`);
}
const basePath = getBasePath();
return [
{
extraOpenTags: `<a href="/t/${encodeURIComponent(hashTitle)}">`,
extraOpenTags: `<a href="${basePath}/t/${encodeURIComponent(hashTitle)}">`,
extraCloseTags: "</a>",
cls: modifiedCls,
},
Expand Down
4 changes: 3 additions & 1 deletion src/pad/static/js/result.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SearchResponse } from "ep_search/setup";
import { getBasePath } from "./util";

export function escapeForText(query: string): string {
let escaped = query;
Expand Down Expand Up @@ -34,8 +35,9 @@ export function query(
if (rows !== undefined) {
opts.push(`&rows=${rows}`);
}
const basePath = getBasePath();
$.getJSON(
`/search/?query=${encodeURIComponent(query)}${opts.join("")}`,
`${basePath}/search/?query=${encodeURIComponent(query)}${opts.join("")}`,
(data: SearchResponse) => {
resolve(data);
}
Expand Down
5 changes: 4 additions & 1 deletion src/pad/static/js/toolbar.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getBasePath } from "./util";

export type Callbacks = {
onSearch: (query: string) => void;
onSort: (sort: string) => void;
Expand Down Expand Up @@ -82,8 +84,9 @@ function createSearchBox(
if (typeof query !== "string") {
return;
}
const basePath = getBasePath();
if (!onCreate) {
window.open(`/t/${encodeURIComponent(query)}`, "_blank");
window.open(`${basePath}/t/${encodeURIComponent(query)}`, "_blank");
return;
}
onCreate(query);
Expand Down
19 changes: 19 additions & 0 deletions src/pad/static/js/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function getBasePath() {
// The path is in the form of .../p/id or .../t/title, so get the part before that
const path = window.location.pathname;
let index = path.indexOf("/p/");
if (index < 0) {
index = path.indexOf("/t/");
if (index < 0) {
console.warn("Base path not found", path);
// remove the last part of the path
const lastSlash = path.lastIndexOf("/");
if (lastSlash >= 0) {
return path.substring(0, lastSlash);
} else {
return "";
}
}
}
return path.substring(0, index);
}
7 changes: 5 additions & 2 deletions src/pad/templates/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<link rel="stylesheet" href="../static/plugins/ep_weave/static/css/index.css" type="text/css" />
<link rel="stylesheet" href="../static/plugins/ep_weave/static/css/styles.css" type="text/css" />
<link rel="stylesheet" href="./static/plugins/ep_weave/static/css/index.css" type="text/css" />
<link rel="stylesheet" href="./static/plugins/ep_weave/static/css/styles.css" type="text/css" />

<div id="weave-index">
<div class="weave-toolbar">
Expand All @@ -16,6 +16,9 @@

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

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

0 comments on commit 072ff8f

Please sign in to comment.