Various AT Protocol integrations with obsidian
20
fork

Configure Feed

Select the types of activity you want to include in your feed.

replace wikilinks with markdown links

+69 -5
+6 -4
src/commands/publishDocument.ts
··· 1 1 import { Notice, TFile } from "obsidian"; 2 2 import type AtmospherePlugin from "../main"; 3 - import { createDocument, putDocument, getPublication, markdownToLeafletContent, stripMarkdown, markdownToPcktContent, buildDocumentUrl } from "../lib"; 3 + import { createDocument, putDocument, getPublication, markdownToLeafletContent, stripMarkdown, markdownToPcktContent, buildDocumentUrl, resolveWikilinks } from "../lib"; 4 4 import { PublicationSelection, SelectPublicationModal } from "../components/selectPublicationModal"; 5 5 import { type ResourceUri, } from "@atcute/lexicons"; 6 6 import { SiteStandardDocument, SiteStandardPublication } from "@atcute/standard-site"; ··· 137 137 throw new Error("Missing publication URI."); 138 138 } 139 139 140 + const resolved = resolveWikilinks(content, plugin.app); 141 + 140 142 // TODO: determine which lexicon to use for rich content 141 143 // for now just check url 142 - let textContent = stripMarkdown(content); 144 + let textContent = stripMarkdown(resolved); 143 145 144 146 let richContent: PubLeafletContent.Main | BlogPcktContent.Main | null = null; 145 147 if (pub?.url.contains("leaflet.pub")) { 146 - richContent = markdownToLeafletContent(content) 148 + richContent = markdownToLeafletContent(resolved) 147 149 } else if (pub?.url.contains("pckt.blog")) { 148 - richContent = markdownToPcktContent(content) 150 + richContent = markdownToPcktContent(resolved) 149 151 } 150 152 151 153 let record = {
+1
src/lib.ts
··· 38 38 stripMarkdown, 39 39 markdownToLeafletContent, 40 40 markdownToPcktContent, 41 + resolveWikilinks, 41 42 } from "./lib/markdown"; 42 43 43 44 export type ATRecord<T> = Record & { value: T };
+1
src/lib/markdown/index.ts
··· 52 52 53 53 export { markdownToPcktContent, pcktContentToMarkdown } from "./pckt"; 54 54 export { markdownToLeafletContent, leafletContentToMarkdown } from "./leaflet"; 55 + export { resolveWikilinks } from "./wikilinks"; 55 56
+60
src/lib/markdown/wikilinks.ts
··· 1 + import type { App } from "obsidian"; 2 + 3 + // Matches [[Note]], [[Note|Alias]], [[Note#Heading]], [[Note#Heading|Alias]] 4 + const WIKILINK_RE = /\[\[([^\]|#]+)(?:#([^\]|]+))?(?:\|([^\]]+))?\]\]/g; 5 + 6 + function titleToSlug(title: string): string { 7 + return title 8 + .toLowerCase() 9 + .trim() 10 + .replace(/\s+/g, "-") 11 + .replace(/[^a-z0-9-]/g, ""); 12 + } 13 + 14 + /** 15 + * Resolves Obsidian wikilinks to standard markdown links. 16 + * 17 + * For each [[Note]] or [[Note|Alias]]: 18 + * - if the linked note has a published `url` in its frontmatter, use it. 19 + * - Otherwise, return the display text without a link 20 + * TODO: return relative url to current note publication? 21 + */ 22 + export function resolveWikilinks( 23 + markdown: string, 24 + app: App, 25 + ): string { 26 + return markdown.replace( 27 + WIKILINK_RE, 28 + (_match, noteName: string, heading: string | undefined, alias: string | undefined) => { 29 + const displayText = (alias ?? noteName).trim(); 30 + const name = noteName.trim(); 31 + 32 + const files = app.vault.getMarkdownFiles(); 33 + const file = files.find( 34 + (f) => f.basename === name || f.path === name + ".md" 35 + ); 36 + 37 + let baseUrl: string | undefined; 38 + 39 + if (file) { 40 + const fm = app.metadataCache.getFileCache(file)?.frontmatter; 41 + if (!fm?.atDocument || !fm?.url) { 42 + return displayText; 43 + } 44 + baseUrl = fm.url as string; 45 + } 46 + 47 + // if (!baseUrl && gardenBaseUrl) { 48 + // const base = gardenBaseUrl.replace(/\/$/, ""); 49 + // baseUrl = `${base}/notes/${titleToSlug(name)}`; 50 + // } 51 + 52 + if (!baseUrl) { 53 + return displayText; 54 + } 55 + 56 + const url = heading ? `${baseUrl}#${titleToSlug(heading)}` : baseUrl; 57 + return `[${displayText}](${url})`; 58 + } 59 + ); 60 + }
+1 -1
src/util.ts
··· 28 28 ); 29 29 imageCache.set(url, match?.[1] ?? ""); 30 30 return match?.[1]; 31 - } catch (e) { 31 + } catch { 32 32 return undefined; 33 33 } 34 34 }