Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

817 B
Raw Blame
import TurndownService from "turndown";
import { gfm } from "turndown-plugin-gfm";
import { jiraRules } from "./html-rules.js";

export type HtmlToMarkdown = (html: string | null | undefined) => string;

export function createJiraHtmlToMarkdown(baseUrl: string): HtmlToMarkdown {
  const service = new TurndownService({
    headingStyle: "atx",
    codeBlockStyle: "fenced",
    bulletListMarker: "-",
    hr: "---",
  });
  service.use(gfm);
  for (const [name, rule] of jiraRules(originOf(baseUrl))) {
    service.addRule(name, rule);
  }

  return (html) => {
    if (!html?.trim()) return "";
    return service
      .turndown(html)
      .replace(/\n{3,}/g, "\n\n")
      .trim();
  };
}

function originOf(baseUrl: string): string {
  try {
    return new URL(baseUrl).origin;
  } catch {
    return "";
  }
}