diff --git a/src/web/cache.ts b/src/web/cache.ts index 7c66cbe6e..015188ccc 100644 --- a/src/web/cache.ts +++ b/src/web/cache.ts @@ -234,7 +234,12 @@ export class DocumentCache implements IDocumentCache { this.$processed[id] = data; const { oxa } = data.frontmatter ?? {}; if (oxa && typeof oxa === 'string') { - this.$links[oxa] = `/${id}`; + this.$links[oxa] = { + url: `/${id}`, + title: data.frontmatter.title || undefined, + description: data.frontmatter.description || undefined, + thumbnail: undefined, + }; } } diff --git a/src/web/transforms/links.ts b/src/web/transforms/links.ts index bc3e8bf29..a51622807 100644 --- a/src/web/transforms/links.ts +++ b/src/web/transforms/links.ts @@ -2,10 +2,17 @@ import { oxaLink, oxaLinkToId } from '@curvenote/blocks'; import { GenericNode, selectAll } from 'mystjs'; import { Root } from './types'; -export type LinkLookup = Record; +type LinkInfo = { + url: string; + title?: string; + description?: string; + thumbnail?: string; +}; + +export type LinkLookup = Record; export function transformLinks(mdast: Root, lookup: LinkLookup): boolean { - const links = selectAll('link', mdast) as GenericNode[]; + const links = selectAll('link,linkBlock', mdast) as GenericNode[]; if (links.length === 0) return false; let changed = 0; links.forEach((link) => { @@ -13,12 +20,18 @@ export function transformLinks(mdast: Root, lookup: LinkLookup): boolean { if (!oxa) return; link.oxa = oxa; const key = oxaLink(oxa, false) as string; - const url = lookup[key]; + const url = lookup[key].url; if (url && url !== link.url) { changed += 1; // the `internal` flag is picked up in the link renderer (prefetch!) link.internal = true; link.url = url; + if (link.type === 'linkBlock') { + // Any values already present on the block override link info + link.title = link.title || lookup[key].title; + link.children = link.children || [{ type: 'text', value: lookup[key].description }]; + link.thumbnail = link.thumbnail || lookup[key].thumbnail; + } } }); return changed > 0;