Skip to content

Commit

Permalink
Add route for simple wiki links
Browse files Browse the repository at this point in the history
  • Loading branch information
MelanX committed Dec 27, 2024
1 parent c124ca9 commit 2607546
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 2 deletions.
32 changes: 30 additions & 2 deletions docusaurus.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ const config: Config = {

plugins: [
require.resolve('docusaurus-lunr-search'),
async function simpleWikiLink(context, options) {
return {
name: "simple-wiki-link-plugin",
async contentLoaded({actions}) {
const {addRoute} = actions;
addRoute({
path: "/swl/:slug",
component: "@site/src/components/SimpleWikiLink",
exact: true,
});
},
};
},
],

presets: [
Expand All @@ -41,8 +54,23 @@ const config: Config = {
includeCurrentVersion: isDev,
versions: {
'1.16.x': {
banner: 'unmaintained'
}
banner: 'none'
},
'1.17.x': {
banner: 'none'
},
'1.18.x': {
banner: 'none'
},
'1.19.x': {
banner: 'none'
},
'1.20.x': {
banner: 'none'
},
'1.21.x': {
banner: 'none'
},
}
},
blog: {
Expand Down
75 changes: 75 additions & 0 deletions src/components/SimpleWikiLink/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import React, {useEffect} from "react";
import {useParams} from "react-router-dom";
import {useAllDocsData} from "@docusaurus/plugin-content-docs/client";
import Layout from '@theme/Layout';
import clsx from "clsx";
import Heading from "@theme/Heading";

interface RouteParams {
slug: string;
}

const SimpleWikiLink = () => {
const {slug: slugToSearch} = useParams<RouteParams>();
const allDocsData = useAllDocsData();

useEffect(() => {
if (!slugToSearch || !allDocsData) {
console.error("No 'slug' provided in query parameters or docs data unavailable.");
return;
}

let docPath = null;
for (const docData of Object.values(allDocsData)) {
const sortedVersions = [...docData.versions].sort((a, b) =>
a.name.localeCompare(b.name)
);

for (let i = sortedVersions.length - 1; i >= 0; i--) {
const version = sortedVersions[i];
const matchingDoc = version.docs.find((doc) => {
const originalSlug = doc.id.startsWith("wiki/mods/")
? doc.id.replace("wiki/mods/", "")
: null;

if (!originalSlug) return false;

const slug = originalSlug.endsWith("/index")
? originalSlug.replace("/index", "")
: originalSlug;

return slug === slugToSearch;
});

if (matchingDoc) {
docPath = matchingDoc.path;
break;
}
}
}

console.log(`Found docPath: ${docPath}`);
if (docPath) {
window.location.href = docPath;
} else {
console.error("No 'slug' provided in query parameters.");
}
}, [slugToSearch, allDocsData]);

return (
<Layout>
<main className={clsx('container margin-vert--xl')}>
<div className="row">
<div className="col col--6 col--offset-3">
<Heading as="h1" className="hero__title">
Searching wiki page for <code>{slugToSearch}</code>...
</Heading>
<p>If you don't get redirected, I'm sorry. Search for yourself &lt;3</p>
</div>
</div>
</main>
</Layout>
);
};

export default SimpleWikiLink;

0 comments on commit 2607546

Please sign in to comment.