Skip to content

Wikilinks

Manual installation

  1. Install BrainDB

  2. Install @braindb/remark-wiki-link

    Terminal window
    pnpm add @braindb/remark-wiki-link
  3. Configure Astro

    astro.config.mjs
    import remarkWikiLink from "@braindb/remark-wiki-link";
    import { brainDbAstro, getBrainDb } from "@braindb/astro";
    const bdb = getBrainDb();
    await bdb.ready();
    export default defineConfig({
    integartions: [brainDbAstro({ remarkWikiLink: false })],
    markdown: {
    remarkPlugins: [
    [
    remarkWikiLink,
    {
    linkTemplate: ({ slug, alias }) => {
    const [slugWithoutAnchor, anchor] = slug.split("#");
    if (slugWithoutAnchor) {
    const doc = bdb.documentsSync({ slug: slugWithoutAnchor })[0];
    if (doc) {
    if (!doc.frontmatter().draft || import.meta.env.DEV) {
    return {
    hName: "a",
    hProperties: {
    href: anchor ? `${doc.url()}#${anchor}` : doc.url(),
    class: doc.frontmatter().draft ? "draft-link" : "",
    },
    hChildren: [
    {
    type: "text",
    value:
    alias == null ? doc.frontmatter().title : alias,
    },
    ],
    };
    }
    }
    }
    return {
    hName: "span",
    hProperties: {
    class: "broken-link",
    title: `Can't resolve link to ${slug}`,
    },
    hChildren: [{ type: "text", value: alias || slug }],
    };
    },
    },
    ],
    ],
    },
    });

Example

[[backlinks]] [[404|Example of broken link]]

Backlinks Example of broken link

Further Improvements

  • Anchors in wikilinks ([[page#anchor]], [[page#anchor|alias]])
    • Do we need to URL-encode anchors?
    • Do we need to slugify anchors?
  • Check that anchors correspond to a header in the target document.
  • What about ambiguous links (bdb.documentsSync({ slug: permalink }).length > 1)?
  • Image wikilinks (![[some.jpg]])