From d4d6cd4431d9bd603fdd8b6f794bd5137ac01f76 Mon Sep 17 00:00:00 2001 From: Tony Ivanov Date: Tue, 2 Jul 2024 11:57:06 +0200 Subject: [PATCH] Begun #8 ,added script for orgmode but hook does not trigger Implemented Orgmode Rendering Cleaned up for patch --- .gitignore | 1 + background.js | 6 ++++-- manifest.json | 3 ++- orgmode.js | 27 +++++++++++++++++++++++++++ package.json | 10 ++++++++-- 5 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 orgmode.js diff --git a/.gitignore b/.gitignore index 3270146..8883822 100644 --- a/.gitignore +++ b/.gitignore @@ -109,3 +109,4 @@ bundle-json.js bundle-markdown.js bundle-ssb.js bundle-reader.js +bundle-orgmode.js diff --git a/background.js b/background.js index 0253dfb..3b6c1e4 100644 --- a/background.js +++ b/background.js @@ -15,14 +15,16 @@ const TYPE_MAP = { // TODO: Hooks for activitystream rendering? 'application/activity+json': 'json', 'application/ld+json': 'json', - 'application/ssb+json': 'ssb' + 'application/ssb+json': 'ssb', + 'text/x-org': 'orgmode' } const SCRIPT_MAP = { markdown: scriptURL('markdown'), gemini: scriptURL('gemini'), json: scriptURL('json'), - ssb: scriptURL('ssb') + ssb: scriptURL('ssb'), + orgmode: scriptURL('orgmode') } const code = ` diff --git a/manifest.json b/manifest.json index 1041816..da4bfff 100644 --- a/manifest.json +++ b/manifest.json @@ -28,6 +28,7 @@ "bundle-markdown.js", "bundle-json.js", "bundle-ssb.js", - "bundle-reader.js" + "bundle-reader.js", + "bundle-orgmode.js" ] } diff --git a/orgmode.js b/orgmode.js new file mode 100644 index 0000000..303918a --- /dev/null +++ b/orgmode.js @@ -0,0 +1,27 @@ +/* global location */ +import renderPage from './template.js' +import { unified } from 'unified' +import parse from 'uniorg-parse' +import uniorg2rehype from 'uniorg-rehype' +import stringify from 'rehype-stringify' + +export async function renderOrg (orgText) { + const processor = unified() + .use(parse) + .use(uniorg2rehype) + .use(stringify) + + const { value: html } = await processor.process(orgText) + + const { pathname } = location + const filename = pathname.substring(pathname.lastIndexOf('/') + 1) + + // extract first heading as page title, fallback onto filename + const pageTitle = html.match(/([^<]+)<\/h\d>/)?.[1] || filename + + renderPage(html, pageTitle) +} + +const text = document.querySelector('pre').innerText +renderOrg(text) + .catch(err => console.error('Rendering orgmode failed:', err)) diff --git a/package.json b/package.json index 57323c5..7384ac7 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "build-gemini": "browserify ./gemini.js > bundle-gemini.js", "build-ssb": "browserify ./ssb.js > bundle-ssb.js", "build-reader": "browserify ./reader.js > bundle-reader.js", - "build": "npm run build-markdown && npm run build-json && npm run build-gemini && npm run build-ssb && npm run build-reader" + "build-org": "esbuild --bundle orgmode.js --outfile=bundle-orgmode.js", + "build": "npm run build-markdown && npm run build-json && npm run build-gemini && npm run build-ssb && npm run build-reader && npm run build-org" }, "repository": { "type": "git", @@ -25,6 +26,7 @@ "homepage": "https://github.com/RangerMauve/extension-agregore-renderer#readme", "devDependencies": { "browserify": "^17.0.0", + "esbuild": "^0.23.0", "standard": "^17.0.0" }, "dependencies": { @@ -33,10 +35,14 @@ "gemini-to-html": "^2.2.0", "html-to-text": "^8.2.0", "marked": "^4.0.16", + "rehype-stringify": "^10.0.0", "ssb-fetch": "^1.5.2", "ssb-markdown": "^6.0.7", "ssb-ref": "^2.16.0", - "ssb-uri2": "^1.8.1" + "ssb-uri2": "^1.8.1", + "unified": "^11.0.5", + "uniorg-parse": "^3.0.1", + "uniorg-rehype": "^1.2.0" }, "standard": { "ignore": [