diff options
| author | Guillermo Serrahima <dev@vylion.com> | 2026-03-20 21:58:50 +0100 |
|---|---|---|
| committer | Guillermo Serrahima <dev@vylion.com> | 2026-03-20 21:58:50 +0100 |
| commit | ad8a4cdc3ea5040b7732bc9e6e3090690a1032b1 (patch) | |
| tree | 6b1b30b5280142605237f59e600822d60d6e0504 | |
| parent | 004ab7db67c24fed0a1b96d8911ae4a82f4917a1 (diff) | |
| download | llostre-ad8a4cdc3ea5040b7732bc9e6e3090690a1032b1.tar.xz llostre-ad8a4cdc3ea5040b7732bc9e6e3090690a1032b1.zip | |
Convert facets to Markdown only if enabled
| -rw-r--r-- | frontend/src/main.ts | 55 |
1 files changed, 34 insertions, 21 deletions
diff --git a/frontend/src/main.ts b/frontend/src/main.ts index 302fce0..d0cff46 100644 --- a/frontend/src/main.ts +++ b/frontend/src/main.ts @@ -1,18 +1,16 @@ import { marked } from 'marked'; import './style.css' -// Since we parse Markdown after Facets, convert Facets to Markdown and let the Markdown parser create the proper HTML code -// Avoiding partial HTML code possibly interferring with Markdown parsing this way -function convertFacetsToMarkdown(text: string, facets: any[]): string { +// convert Facets to Markdown (before Markdown parsing) if Markdown passing is enabled +// convert them to HTML code otherwise +function convertFacets(text: string, facets: any[], toMarkown: boolean): string { if (!facets || facets.length === 0) { return text; } - // convert text to UTF-8 bytes because Bluesky counts bytes in UTF-8 const encoder = new TextEncoder(); const postBytes = encoder.encode(text); - // sort facets by start position facets.sort((a, b) => a.byteStart - b.byteStart); let result = ''; @@ -20,27 +18,42 @@ function convertFacetsToMarkdown(text: string, facets: any[]): string { const decoder = new TextDecoder(); for (const facet of facets) { - // add text before this facet const beforeBytes = postBytes.slice(lastPos, facet.byteStart); result += decoder.decode(beforeBytes); - // add the faceted text as a link const facetBytes = postBytes.slice(facet.byteStart, facet.byteEnd); const facetText = decoder.decode(facetBytes); - switch (facet.type) { - case 'link': - result += `[${facetText}](${facet.value})`; - break; - case 'mention': - result += `[@${facetText}](https://bsky.app/profile/${facet.value})`; - break; - case 'tag': - result += `[#${facetText}](https://bsky.app/hashtag/${facet.value})`; - break; - default: // in case Bluesky ever adds other facet types - result += facetText; // if we don't recognize the type, just add it as plain text - break; + if (toMarkown) { + switch (facet.type) { + case 'link': + result += `[${facetText}](${facet.value})`; + break; + case 'mention': + result += `[@${facetText}](https://bsky.app/profile/${facet.value})`; + break; + case 'tag': + result += `[#${facetText}](https://bsky.app/hashtag/${facet.value})`; + break; + default: // in case Bluesky ever adds other facet types + result += facetText; // if we don't recognize the type, just add it as plain text + break; + } + } else { + switch (facet.type) { + case 'link': + result += `<a href="${facet.value}" target="_blank">${facetText}</a>`; + break; + case 'mention': + result += `<a href="https://bsky.app/profile/${facet.value}" target="_blank">${facetText}</a>`; + break; + case 'tag': + result += `<a href="https://bsky.app/hashtag/${facet.value}" target="_blank">#${facetText}</a>`; + break; + default: + result += facetText; + break; + } } lastPos = facet.byteEnd; @@ -98,7 +111,7 @@ async function main() { for (const note of notes) { let parsedText = ''; if(note.facets && note.facets.length > 0) { - parsedText = convertFacetsToMarkdown(note.text || '', note.facets || []); + parsedText = convertFacets(note.text || '', note.facets || [], true); } else { parsedText = note.text; } |
