From 09616dd301602819a81f44ebc8f796f1a68bac30 Mon Sep 17 00:00:00 2001 From: Franklin Koch Date: Tue, 19 Apr 2022 14:12:02 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=96=87=20=20Add=20linkBlock=20transformat?= =?UTF-8?q?ion=20and=20title/desc/thumb=20to=20link=20cache=20(#58)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update mystjs. Add more definitive return type * Add linkBlock transformation and title/desc/thumb to link cache Co-authored-by: Yuxi Wang --- src/web/cache.ts | 7 ++++++- src/web/transforms/links.ts | 19 ++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) 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;