Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions .github/site/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,32 @@ mermaid.initialize({ startOnLoad: false });
* @param {string} path Absolute path (/ points to the repository root) to the resource
* @returns {string} The URL to the resource
*/
const urlFor = (type, path) => `https://github.com/exteditor/ghostbird/${type}/main/${encodeURIComponent(path.replace(/^[/]+/, ''))}`;
const urlFor = (type, path) => `https://github.com/exteditor/ghostbird/${encodeURIComponent(type)}/main/${encodeURIComponent(path.replace(/^[/]+/, ''))}`;

/**
* Build a Markdown text that redirects to a URL
* @param {string} url The URL to redirect to
* @returns {string} a Markdown text
* @returns {string} A Markdown text
*/
const redirectTo = (url) => `Redirecting to ${url}...\n\n<script>\nlocation.href = "${url}"${'</'}script>`;
const redirectTo = (url) => JSON.stringify({redirectTo: url});

/**
* Does redirect if the text is an instruction for it.
* @param {string} text A Markdown text that may contain a redirect instruction
* @returns {string} Returns the `text` as-is if it doesn't contain redirect instruction
*/
function tryRedirect(text) {
try {
if (text.startsWith('{"redirectTo":"')) {
const { redirectTo } = JSON.parse(text);
location.replace(redirectTo);
return "Redirecting...";
}
} catch (e) {
console.warn("failed to parse the redirect instruction", e);
}
return text;
}

/**
* Add a footer to the Markdown text
Expand Down Expand Up @@ -46,7 +64,7 @@ window.$docsify = {
logo: 'ext/blue.svg',
formatUpdated: '{YYYY}-{MM}-{DD}',
relativePath: true,
executeScript: true,
executeScript: false,
homepage: "homepage.md",
coverpage: "coverpage.md",
loadNavbar: "navbar.md",
Expand All @@ -56,6 +74,7 @@ window.$docsify = {
themeColor: '#0b9dd6',
routes: {
'/README': (route) => redirectTo(urlFor('blob', `${route}.md`)),
'/LICENSE': (route) => redirectTo(urlFor('blob', route)),
'/[-._/a-zA-Z]*[.][a-zA-Z]+$': (route) => redirectTo(urlFor('blob', route)),
'/[-._/a-zA-Z]+/$': (route) => redirectTo(urlFor('tree', route)),
},
Expand All @@ -80,6 +99,7 @@ window.$docsify = {
},
plugins: [
(hook, vm) => {
hook.beforeEach((text) => tryRedirect(text));
hook.beforeEach((text) => addFooter(text, vm));
hook.doneEach(() => mermaid.run());
},
Expand Down
2 changes: 1 addition & 1 deletion src/app-options/options_event_router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class OptionsEventRouter {

constructor(readonly optionsStore: IOptionsStore) {}

initOptions(elem: GhostbirdOptionsElement): Promise<void> {
initOptions(elem: GhostbirdOptionsElement): Promise<never> {
return elem.startSync(this.optionsStore)
}
}
22 changes: 15 additions & 7 deletions src/root/options.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
/** @file Entrypoint of the options page */

import { GhostbirdOptionsElement } from "src/app-options/ghostbird_options_element"
import { OptionsEventRouter } from "src/app-options/options_event_router"
import { GhostbirdOptionsElement, OptionsEventRouter } from "src/app-options"
import { default as optionsSyncCtor } from "webext-options-sync"
import { startupOptions } from "./startup/startup_options"

/** Initialize the event router for the options page */
function prepareRouter(): OptionsEventRouter {
const startup = startupOptions({
optionsSyncCtor,
})
return startup(OptionsEventRouter)
}

console.log("starting", import.meta.url)
/** Wait for the options page to be ready and start processing events */
async function runRouterAsync(): Promise<never> {
await customElements.whenDefined("ghostbird-options")

customElements.whenDefined("ghostbird-options").then(async () => {
const router = prepareRouter()
let [opt] = document.getElementsByTagName("ghostbird-options")
if (!opt) {
throw Error("unexpected html state")
}
await router.initOptions(opt as GhostbirdOptionsElement)
})

return router.initOptions(opt as GhostbirdOptionsElement)
}

console.info("starting", import.meta.url)

const mainPromise: Promise<never> = runRouterAsync()

customElements.define("ghostbird-options", GhostbirdOptionsElement)

console.log("started", import.meta.url)
console.info("started", import.meta.url)

await mainPromise