Backlinks #1335
Replies: 2 comments 2 replies
-
Interesting idea. I think it might even be possible with a user plugin or component. This is a potentially performance intensive task — you have to extract all links for every page and query those — but if structured in a way that ensures it only runs once, it could be ok. For large sites, it would still be a bit of a dev server hit: every time you edit a page, you'd have to recalculate for the full site graph again. That makes me a bit hesitant to think of this as a core built-in feature, but I'd be happy to help anyone looking to publish this as a third-party add-on. Quick sketch of how it might work: // link-map.ts
import { getCollection } from 'astro:content';
// Calculate the map of site links at the top-level so it doesn't run more than needed
export const linkMap = await generateLinkMap()
async function generateLinkMap() {
const map = new Map<Link, Set<Page>>();
const pages = await getCollection('docs');
for (const page of pages) {
// Extract each link in this page with your preferred strategy
const links = extractLinks(page);
for (const link of links) {
const pagesWithThisLink = map.get(link) || new Set<Page>();
pagesWithThisLink.add(page);
map.set(link, pagesWithThisLink);
}
}
return map;
} Component that can use this. Could be added with an override to Starlight's footer, or used inline in MDX content: ---
import { linkMap } from './link-map';
const { currentPage } = Astro.props;
const backlinks = linkMap.get(currentPage);
---
<h2>Backlinks</h2>
{backlinks.size ? (
backlinks.map(link => <SomeMarkup />)
) : (
<p>No backlinks found</p>
)} A few parts of the code still need fleshing out here obviously, most importantly how would |
Beta Was this translation helpful? Give feedback.
-
I have experimental project which may help with the task. Basic idea: file watcher + markdown parser + db = "markdown-graph-content-layer-database". Usage looks something like this: const bdb = new BrainDB({
root: path.resolve(process.cwd(), "src/content/docs"),
url: (filePath, _frontmatter) => `${generateSlug(filePath)}/`,
});
bdb.start(); ---
import { bdb } from "../lib/braindb";
const { entry } = Astro.props;
const doc = await bdb.findDocument(`/${entry.id}`);
---
<div>
<b>Backlinks:</b>
{
doc &&
doc.documentsFrom().map((x) => (
<li>
<a href={x.url()}>{x.title()}</a>
</li>
))
}
</div> Full example is here: https://astro-digital-garden.stereobooster.com/recipes/backlinks/ |
Beta Was this translation helpful? Give feedback.
-
What version of
starlight
are you using?0.15.2
What is your idea?
Could you add support for backlinks?
Automatically list links to pages that reference the current page you are on.
Why is this feature necessary?
Backlinks help readers in exploring a knowledge graph.
Do you have examples of this feature in other projects?
Participation
Beta Was this translation helpful? Give feedback.
All reactions