Skip to content

Instantly share code, notes, and snippets.

@ThePlenkov
Last active May 22, 2025 08:14
Show Gist options
  • Save ThePlenkov/f800bc78cb33fc489d457ab8a4751413 to your computer and use it in GitHub Desktop.
Save ThePlenkov/f800bc78cb33fc489d457ab8a4751413 to your computer and use it in GitHub Desktop.
Convert markdown to Confluence XHTML
import {MarkdownTransformer} from "@atlaskit/editor-markdown-transformer";
import {defaultSchema} from "@atlaskit/adf-schema/schema-default";
import {ConfluenceTransformer} from "@atlaskit/editor-confluence-transformer";
import {JSDOM} from "jsdom";
test("Create a new page", async () => {
const md = new MarkdownTransformer(defaultSchema);
const ct = new ConfluenceTransformer(defaultSchema);
const mdADF = md.parse("# Hello!");
const dom = new JSDOM(
"<!DOCTYPE html><html><head></head><body></body></html>"
);
global.document = dom.window.document;
const mdXML = ct.encode(mdADF);
expect(mdXML).toBe("<h1>Hello!</h1>");
}, 100000);
@mac2000
Copy link

mac2000 commented May 22, 2025

Thank you for such a great gist 💪

Here is one more, in case if anyone will be landed here

In my case, I was looking for a way to create markdown from Jira ticket description

At moment it is semi doable

Here is a snippet:

import { describe, it, expect } from "vitest";
import { defaultSchema } from "@atlaskit/adf-schema/schema-default";
import { JIRATransformer } from "@atlaskit/editor-jira-transformer";
import { JSONDocNode, JSONTransformer } from "@atlaskit/editor-json-transformer";
import { MarkdownTransformer } from "@atlaskit/editor-markdown-transformer";
import { JSDOM } from "jsdom";

describe("let's pretend we have retrieved Jira ticket via API, it will have description field in Atlassian Document Format (ADF)", () => {
  const doc: JSONDocNode = {
    type: "doc",
    version: 1,
    content: [
      {
        type: "paragraph",
        content: [
          {
            type: "text",
            text: "Hello World",
          },
        ],
      },
    ],
  };

  it("and here is how we can map it to HTML", () => {
    const jiraTransformer = new JIRATransformer(defaultSchema);
    const jsonTransformer = new JSONTransformer(defaultSchema);
    const parsed = jsonTransformer.parse(doc);
    const dom = new JSDOM("<!DOCTYPE html><html><head></head><body></body></html>");
    globalThis.document = dom.window.document;
    const html = jiraTransformer.encode(parsed);
    expect(html).toBe("<p>Hello World</p>");
  });

  it("unfortunatelly markdown is not implemented yet", () => {
    const jsonTransformer = new JSONTransformer(defaultSchema);
    const markdownTransformer = new MarkdownTransformer(defaultSchema);
    const parsed = jsonTransformer.parse(doc);
    expect(() => markdownTransformer.encode(parsed)).toThrowError("This is not implemented yet");
  });
});

as you may see, at moment MarkdownTransformer encode method is not implemented, but still we can create HTML and later, convert it to markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment