all repos — snow-editor @ 13d855e25e8510a57608b0ce77f62cbeed372d0e

small and cozy markdown, and orgmode editor

src/lib/org/pipeline.js (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
import { unified } from 'unified';
import reorgParse from '@orgajs/reorg-parse';
import reorgRehype from '@orgajs/reorg-rehype';
import rehypeStringify from 'rehype-stringify';
import { enhanceOrgHtml } from './enhanceHtml.js';
import { normalizeOrgContent } from './normalize.js';
import { sanitizeOrgHtml } from './sanitize.js';

let processor = null;

function getProcessor() {
  if (!processor) {
    processor = unified()
      .use(reorgParse)
      .use(reorgRehype)
      .use(rehypeStringify);
  }
  return processor;
}

function processOrg(content) {
  return getProcessor().processSync(normalizeOrgContent(content)).toString();
}

export function orgToHtmlSync(content) {
  if (!content || !content.trim()) return '';
  const raw = enhanceOrgHtml(processOrg(content));
  return sanitizeOrgHtml(raw);
}

export function orgToRawHtmlSync(content) {
  if (!content || !content.trim()) return '';
  return processOrg(content);
}