unified#unified TypeScript Examples

The following examples show how to use unified#unified. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: markdown.ts    From revolt.chat with Mozilla Public License 2.0 6 votes vote down vote up
renderMarkdown = async (markdown: string) => {
    const result = await unified()
        .use(remarkParse)
        .use(remarkGfm)
        .use(remarkRehype, {allowDangerousHtml: true})
        .use(rehypeStringify, {allowDangerousHtml: true})
        .process(markdown)
    return result.toString()
}
Example #2
Source File: index.spec.ts    From remark-slate-transformer with MIT License 6 votes vote down vote up
describe("e2e", () => {
  const toSlateProcessor = unified()
    .use(markdown)
    .use(gfm)
    .use(footnotes, { inlineNotes: true })
    .use(frontmatter, ["yaml", "toml"])
    .use(math)
    .use(remarkToSlate);
  const toRemarkProcessor = unified()
    .use(slateToRemark)
    .use(gfm)
    .use(footnotes, { inlineNotes: true })
    .use(frontmatter, ["yaml", "toml"])
    .use(math)
    .use(stringify, { bullet: "-", emphasis: "_" });

  const fixturesDir = path.join(__dirname, FIXTURE_PATH);
  const filenames = fs.readdirSync(fixturesDir);
  filenames.forEach((filename) => {
    it(filename, () => {
      const slateNodes = toSlateProcessor.processSync(
        fs.readFileSync(path.join(fixturesDir, filename))
      ).result;
      expect(slateNodes).toMatchSnapshot();

      const mdastTree = toRemarkProcessor.runSync({
        type: "root",
        children: slateNodes,
      } as any);
      expect(mdastTree).toMatchSnapshot();

      const text = toRemarkProcessor.stringify(mdastTree);
      expect(text).toMatchSnapshot();
    });
  });
});
Example #3
Source File: index.spec.ts    From remark-slate-transformer with MIT License 6 votes vote down vote up
describe("e2e legacy", () => {
  const toSlateProcessor = unified()
    .use(markdown)
    .use(gfm)
    .use(footnotes, { inlineNotes: true })
    .use(frontmatter, ["yaml", "toml"])
    .use(math)
    .use(remarkToSlateLegacy);
  const toRemarkProcessor = unified()
    .use(slateToRemarkLegacy)
    .use(gfm)
    .use(footnotes, { inlineNotes: true })
    .use(frontmatter, ["yaml", "toml"])
    .use(math)
    .use(stringify, { bullet: "-", emphasis: "_" });

  const fixturesDir = path.join(__dirname, FIXTURE_PATH);
  const filenames = fs.readdirSync(fixturesDir);
  filenames.forEach((filename) => {
    it(filename, () => {
      const value: any = toSlateProcessor.processSync(
        fs.readFileSync(path.join(fixturesDir, filename))
      ).result;
      expect(value).toMatchSnapshot();
      expect(Value.fromJSON(value)).toMatchSnapshot();

      const mdastTree = toRemarkProcessor.runSync({
        type: "root",
        children: value.document.nodes,
      } as any);
      expect(mdastTree).toMatchSnapshot();

      const text = toRemarkProcessor.stringify(mdastTree);
      expect(text).toMatchSnapshot();
    });
  });
});
Example #4
Source File: org-to-hast.spec.ts    From uniorg with GNU General Public License v3.0 6 votes vote down vote up
process = (input: string, options?: Partial<OrgToHastOptions>) => {
  const processor = unified()
    .use(orgParse)
    .use(org2rehype, options)
    .use(raw)
    .use(format)
    .use(html);

  return String(processor.processSync(input));
}
Example #5
Source File: index.spec.ts    From uniorg with GNU General Public License v3.0 6 votes vote down vote up
process = (s: string, options?: Options): VFile => {
  const processor = unified().use(uniorg).use(extractKeywords, options);

  const f = new VFile(s);
  // not interested in result
  processor.runSync(processor.parse(f), f);

  return f;
}
Example #6
Source File: index.spec.ts    From uniorg with GNU General Public License v3.0 6 votes vote down vote up
process = (s: string, options?: Partial<Options>) => {
  const processor = unified()
    .use(uniorg)
    .use(uniorgAttach, options)
    .use(function (this: any) {
      this.Compiler = (x: any) => x;
    });

  return processor.processSync(s).result;
}
Example #7
Source File: playground-legacy.stories.tsx    From remark-slate-transformer with MIT License 5 votes vote down vote up
toSlateProcessor = unified()
  .use(markdown)
  .use(gfm)
  .use(frontmatter)
  .use(remarkToSlateLegacy)
Example #8
Source File: index.spec.ts    From uniorg with GNU General Public License v3.0 5 votes vote down vote up
processor = unified().use(uniorgParse).use(uniorgStringify)
Example #9
Source File: index.spec.ts    From uniorg with GNU General Public License v3.0 5 votes vote down vote up
process = (s: string, options?: Options): Node => {
  const processor = unified().use(uniorg).use(uniorgSlug, options);

  const f = new VFile(s);

  return processor.runSync(processor.parse(f), f);
}
Example #10
Source File: playground-legacy.stories.tsx    From remark-slate-transformer with MIT License 5 votes vote down vote up
toRemarkProcessor = unified()
  .use(slateToRemarkLegacy)
  .use(gfm)
  .use(frontmatter)
  .use(stringify)
Example #11
Source File: Markdown.tsx    From project-loved-web with MIT License 5 votes vote down vote up
processor = unified().use(remarkParse).use(osuWikiLinks).use(remarkHtml).freeze()
Example #12
Source File: playground-current.stories.tsx    From remark-slate-transformer with MIT License 5 votes vote down vote up
toRemarkProcessor = unified()
  .use(slateToRemark)
  .use(gfm)
  .use(frontmatter)
  .use(stringify)
Example #13
Source File: playground-current.stories.tsx    From remark-slate-transformer with MIT License 5 votes vote down vote up
toSlateProcessor = unified()
  .use(markdown)
  .use(gfm)
  .use(frontmatter)
  .use(remarkToSlate)
Example #14
Source File: index.spec.ts    From remark-slate-transformer with MIT License 5 votes vote down vote up
describe("options", () => {
  it("override builders", () => {
    const mdText = `
  - AAAA
    - BBBB
  - CCCC
    `;
    const toSlateProcessor = unified()
      .use(markdown)
      .use(remarkToSlate, {
        overrides: {
          list: (node, next) => ({
            type: "foo",
            children: next(node.children),
          }),
          text: (node) => ({ type: "bar", bar: node.value }),
        },
      });
    const toRemarkProcessor = unified()
      .use(slateToRemark, {
        overrides: {
          foo: (node: any, next) => ({
            type: "list",
            children: next(node.children),
          }),
          bar: (node: any) => ({ type: "text", value: node.bar }),
        },
      })
      .use(stringify, { bullet: "-" });
    const slateTree = toSlateProcessor.processSync(mdText).result;
    expect(slateTree).toMatchSnapshot();
    const mdastTree = toRemarkProcessor.runSync({
      type: "root",
      children: slateTree,
    } as any);
    expect(mdastTree).toMatchSnapshot();
    const text = toRemarkProcessor.stringify(mdastTree);
    expect(text).toMatchSnapshot();
  });
});
Example #15
Source File: case.ts    From reskript with MIT License 5 votes vote down vote up
serializer = unified().use(stringify)
Example #16
Source File: case.ts    From reskript with MIT License 5 votes vote down vote up
parser = unified().use(parse).use(gfm)
Example #17
Source File: index.spec.ts    From remark-slate-transformer with MIT License 4 votes vote down vote up
describe("issues", () => {
  it("issue42", () => {
    const mdText = `
- list
  - list
- list
- list
- 
  `;
    const toSlateProcessor = unified().use(markdown).use(remarkToSlate);
    const toRemarkProcessor = unified()
      .use(slateToRemark)
      .use(stringify, { bullet: "-" });
    const slateTree = toSlateProcessor.processSync(mdText).result;
    expect(slateTree).toMatchSnapshot();
    const mdastTree = toRemarkProcessor.runSync({
      type: "root",
      children: slateTree,
    } as any);
    const text = toRemarkProcessor.stringify(mdastTree);
    expect(text).toMatchSnapshot();
  });

  it("issue44", () => {
    const slateNodes = [
      {
        type: "paragraph",
        children: [
          {
            text: "Naoki Urasawa is a ",
          },
          {
            text: "Japanese ",
            strong: true,
          },
          {
            text: "manga artist and musician. He has been ",
          },
          {
            emphasis: true,
            text: "drawing ",
          },
          {
            text: "manga since he ",
          },
          {
            strong: true,
            text: " was",
          },
          {
            text: " four years old, and for most of his career has created two series simultaneously. ",
          },
          {
            strong: true,
            text: "So there ",
          },
        ],
      },
    ];
    const toSlateProcessor = unified().use(markdown).use(remarkToSlate);
    const toRemarkProcessor = unified()
      .use(slateToRemark)
      .use(stringify, { emphasis: "*" });
    const mdastTree = toRemarkProcessor.runSync({
      type: "root",
      children: slateNodes,
    } as any);
    expect(mdastTree).toMatchSnapshot();
    const text = toRemarkProcessor.stringify(mdastTree);
    expect(text).toMatchSnapshot();
    const slateTree = toSlateProcessor.processSync(text).result;
    expect(slateTree).toMatchSnapshot();
  });

  it("issue90", () => {
    const slateNodes = [
      {
        type: "paragraph",
        children: [
          {
            text: "Italic",
            strong: true,
            emphasis: true,
          },
          {
            strong: true,
            text: " in a bold paragraph",
          },
        ],
      },
      {
        type: "paragraph",
        children: [
          {
            strong: true,
            text: "This is an ",
          },
          {
            text: "Italic",
            strong: true,
            emphasis: true,
          },
          {
            strong: true,
            text: " in a bold paragraph",
          },
        ],
      },
    ];
    const toSlateProcessor = unified().use(markdown).use(remarkToSlate);
    const toRemarkProcessor = unified()
      .use(slateToRemark)
      .use(stringify, { emphasis: "*" });
    const mdastTree = toRemarkProcessor.runSync({
      type: "root",
      children: slateNodes,
    } as any);
    expect(mdastTree).toMatchSnapshot();
    const text = toRemarkProcessor.stringify(mdastTree);
    expect(text).toMatchSnapshot();
    const slateTree = toSlateProcessor.processSync(text).result;
    expect(slateTree).toMatchSnapshot();
  });
});
Example #18
Source File: org-to-hast.spec.ts    From uniorg with GNU General Public License v3.0 4 votes vote down vote up
describe('org/org-to-hast', () => {
  hastTest('empty', ``);

  hastTest('paragraph', `hello`);

  hastTest('headline', `* hi`);

  hastTest(
    'multiple headlines',
    `* hi
** there
* hello
*** world
`
  );

  hastTest(
    'complex headline',
    `* TODO [#A] headline /italic/ title :some:tags:`
  );

  hastTest(
    'headline with sections',
    `hello
* hi
section
** hello
another section`
  );

  hastTest(
    'does not export :noexport: headline',
    `* hello
some text
* nope :noexport:
not exported text
`
  );

  hastTest(
    'useSections',
    `hello
* headline 1
text
** headline 1.1
more text
* headline 2`,
    { useSections: true }
  );

  hastTest(
    'planning',
    `* headline
CLOSED: [2019-03-13 Wed 23:48] SCHEDULED: [2019-03-13 Wed] DEADLINE: [2019-03-14 Thu]`
  );

  hastTest(
    'property drawer',
    `* headline
:PROPERTIES:
:CREATED: [2019-03-13 Wed 23:57]
:END:`
  );

  hastTest(
    'custom drawer',
    `:MYDRAWER:
hello /there/
:END:`
  );

  hastTest('list', `- hello`);

  hastTest('ordered list', `1. one`);

  hastTest(
    'nested list',
    `
- hello
  - world
  - blah
- hi
`
  );

  hastTest(
    'description list',
    `- term1 :: description 1
- term 2 :: description 2`
  );

  // See https://github.com/rasendubi/uniorg/issues/15
  hastTest(
    'description list with complex tags',
    `
- [[https://example.com][Example]] :: Hello there!
- [[https://github.com][GitHub]] :: This is GitHub, your hub for Git repos.
- *Gitlab* :: Alternative to GitHub
- /Sourcehut/ :: Another alternative to GitHub that primarily uses email-based workflows.
- /Codeberg/ :: *ANOTHER ALTERNATIVE*
- /*self-hosting Git server*/ :: /*The ultimate Git solution for privacy-oriented individuals!*/
`
  );

  hastTest('link', `https://example.com`);

  hastTest('link mixed with text', `hello http://example.com blah`);

  hastTest(
    'escape local link',
    `[[./path with "spaces".org][path with "spaces"]]`
  );

  hastTest(
    'escape local link with percent',
    `[[file:local%2Bpath%2Bwith%2Bpercents.org][local path with percents]]`
  );

  hastTest(
    'do not double-escape urls',
    `[[http://localhost:3000/path%2Bwith%2Bpercents][path with percents]]`
  );

  hastTest(
    'src block',
    `#+begin_src c
,*a = b;
printf("%d\\n", *a);
#+end_src`
  );

  hastTest(
    'verse block',
    `#+BEGIN_VERSE
 Great clouds overhead
 Tiny black birds rise and fall
 Snow covers Emacs

    ---AlexSchroeder
#+END_VERSE`
  );

  hastTest(
    'center block',
    `#+begin_center
hello
#+end_center`
  );

  hastTest(
    'comment block',
    `#+begin_comment
hello
#+end_comment`
  );

  hastTest(
    'remove common src block offset',
    `
  #+begin_src
    hello
      world
  #+end_src`
  );

  hastTest(
    'example block',
    `#+begin_example
example
#+end_example`
  );

  hastTest(
    'export block',
    `#+begin_export
export
#+end_export`
  );

  hastTest(
    'export html block',
    `
#+begin_export html
<abbr title="World Health Organization">WHO</abbr> was founded in 1948.
#+end_export
`
  );

  hastTest(
    'export html keyword',
    `hello
#+HTML: <h1>html tag</h1>`
  );

  hastTest(
    'special block',
    `#+begin_blah
hello
#+end_blah`
  );

  hastTest(
    'html5 fancy block',
    `#+begin_aside
hello
#+end_aside`
  );

  hastTest(
    'blockquote',
    `#+begin_quote
hello, world!
#+end_quote`
  );

  hastTest('keywords', `#+TITLE: blah`);

  hastTest(
    'emphasis',
    `/Consider/ ~t*h*e~ *following* =example= +strike+ _under_`
  );

  hastTest('superscript', `hello^there`);

  hastTest('subscript', `hello_there`);

  hastTest('images', `[[./image.png]]`);

  describe('timestamps', () => {
    hastTest('inactive', `[2021-01-07 Thu]`);
    hastTest('inactive-range', `[2021-01-07 Thu]--[2021-01-08 Fri]`);
    hastTest('active', `<2021-01-07 Thu>`);
    hastTest('active-range', `<2021-01-07 Thu>--<2021-01-09 Sat>`);
    hastTest('with time', `[2021-01-07 Thu 19:36]`);
    hastTest('time range', `[2021-01-07 Thu 19:36-20:38]`);
    hastTest.todo('diary');
  });

  hastTest(
    'table',
    `
| head1  | head2 |
|--------+-------|
| value1 | value2 |
`
  );

  hastTest(
    'table without header',
    `
| value1 | value2 |
| value3 | value4 |
`
  );

  hastTest(
    'table, multiple body',
    `
| head1  | head2 |
|--------+-------|
| value1 | value2 |
| value3 | value4 |
|--------+--------|
| value5 | value6 |
`
  );

  hastTest(
    'table.el table',
    `
+------+
| blah |
+------+
`
  );

  hastTest(
    'comments',
    `hello
# comment
there`
  );

  hastTest('fixed-width', `: hello`);

  hastTest(
    'clock',
    `CLOCK: [2020-12-22 Tue 09:07]--[2020-12-22 Tue 11:10] =>  2:03`
  );

  hastTest(
    `latex environment`,
    `\\begin{hello}
some text
\\end{hello}`
  );

  hastTest('horizontal rule', `-----`);

  hastTest(
    'diary sexp',
    `%%(diary-anniversary 10 31 1948) Arthur's birthday (%d years old)`
  );

  describe('footnotes', () => {
    hastTest(
      'footnote-reference',
      `Some text with a footnote.[fn:1]
  
[fn:1] A very important footnote.`
    );

    hastTest(
      'footnote-definition',
      `Some text with a footnote.[fn:1]
Another footnote [fn:2]    
  
[fn:1] A very important footnote.
[fn:2] Another stellar footnote.`
    );

    hastTest('inline footnote', 'some text[fn:1: footnote definition]');

    hastTest(
      'maintains footnotes order',
      // Note that footnotes are emmitted in order of reference
      `footnote 2[fn:2]
footnote 1[fn:1]

[fn:1] second footnote
[fn:2] first footnote`
    );

    hastTest(
      'does not emit unreferenced footnotes',
      `footnote[fn:1]

[fn:1] hello
[fn:2] unreferenced`
    );

    hastTest('handles missing footnotes', 'footnote[fn:missing]');

    hastTest(
      'inline footnote',
      'footnotes[fn:label: inline footnote definition]'
    );

    hastTest(
      'anonymous inline footnote',
      'footnotes[fn:: inline footnote definition]'
    );
  });

  hastTest(
    'latex-fragment',
    `If $a^2=b$ and \\( b=2 \\), then the solution must be
either $$ a=+\\sqrt{2} $$ or \\[ a=-\\sqrt{2} \\].`
  );

  hastTest('entity', `\\Agrave`);

  test('respects hProperties', () => {
    const s = unified()
      .use(orgParse)
      .use(() => (node: any) => {
        // org-data > section > headline
        const headline = node.children[0].children[0];
        headline.data = { hProperties: { id: 'my-custom-id' } };
      })
      // @ts-ignore for some reason the above does something weird
      .use(org2rehype)
      .use(format)
      .use(html)
      .processSync(`* headline`)
      .toString();

    expect(s).toMatchInlineSnapshot(`

      <h1 id="my-custom-id">headline</h1>

    `);
  });
});
Example #19
Source File: index.spec.ts    From uniorg with GNU General Public License v3.0 4 votes vote down vote up
describe('uniorg-slug', () => {
  test('does not crash on empty document', () => {
    const document = ``;

    process(document);
  });

  test('simple header', () => {
    const document = `* some headline`;

    const n = process(document);

    const h: any = find(n, { type: 'headline' });

    expect(h.data.hProperties.id).toBe('some-headline');
  });

  test('header with formatting', () => {
    const document = `* some /emphasis/ and [[https://example.com][link]]`;

    const n = process(document);

    const h: any = find(n, { type: 'headline' });

    expect(h.data.hProperties.id).toBe('some-emphasis-and-link');
  });

  test('respects CUSTOM_ID', () => {
    const document = `* headline
:PROPERTIES:
:CUSTOM_ID: blah
:END:`;

    const n = process(document);

    const h: any = find(n, { type: 'headline' });

    expect(h.data.hProperties.id).toBe('blah');
  });

  test('with uniorg-rehype', () => {
    const processor = unified()
      .use(uniorg)
      .use(uniorgSlug)
      .use(uniorg2rehype)
      .use(html);

    const s = processor
      .processSync(
        `* headline
** nested headline
:PROPERTIES:
:CUSTOM_ID: blah
:END:
** headline
:PROPERTIES:
:ID: my-id
:END:
~id~ property is ignored.`
      )
      .toString();

    expect(s).toMatchInlineSnapshot(
      `"<h1 id=\\"headline\\">headline</h1><h2 id=\\"blah\\">nested headline</h2><h2 id=\\"headline-1\\">headline</h2><p><code class=\\"inline-code\\">id</code> property is ignored.</p>"`
    );
  });

  test('preserves data.hProperties.id', () => {
    const processor = unified()
      .use(uniorg)
      .use(() => (node) => {
        const headline: any = find(node, { type: 'headline' });
        headline.data = { hProperties: { id: 'my-custom-id' } };
      })
      .use(uniorgSlug)
      .use(uniorg2rehype)
      .use(html);

    const document = `* some headline`;

    const s = String(processor.processSync(document));

    expect(s).toMatchInlineSnapshot(
      `"<h1 id=\\"my-custom-id\\">some headline</h1>"`
    );
  });
});