import { assertEquals } from "testing/asserts.ts"; import { HtmlStrRenderer } from "../ren/html_str.ts"; import { MarkdownParser } from "./md.ts"; const ren = new HtmlStrRenderer(); // Misc Deno.test({ name: "should skip empty line", only: true, fn: () => { const par = new MarkdownParser(); assertEquals(ren.render(par.parse("\n")), ""); assertEquals(ren.render(par.parse("\r\n")), ""); assertEquals(ren.render(par.parse("\n\r\n")), ""); assertEquals(ren.render(par.parse("\n \n")), ""); }, }); // ATX Header Deno.test({ name: "should parse empty ATX header", only: true, fn: () => { const par = new MarkdownParser(); assertEquals(ren.render(par.parse("#")), "

"); assertEquals(ren.render(par.parse("##")), "

"); }, }); Deno.test({ name: "should parse ATX header if line contains additional spaces", only: true, fn: () => { const par = new MarkdownParser(); assertEquals(ren.render(par.parse(" #")), "

"); assertEquals(ren.render(par.parse(" #")), "

"); assertEquals(ren.render(par.parse(" ##")), "

"); assertEquals(ren.render(par.parse(" #")), "

"); assertEquals(ren.render(par.parse(" ##")), "

"); }, }); Deno.test({ name: "should parse ATX header with text", only: true, fn: () => { const par = new MarkdownParser(); assertEquals(ren.render(par.parse("# hello")), "

hello

"); assertEquals(ren.render(par.parse("# hello#")), "

hello#

"); }, }); Deno.test({ name: "should parse ATX header with specific level", fn: () => { const par = new MarkdownParser(); assertEquals(ren.render(par.parse("# hello")), "

hello

"); assertEquals(ren.render(par.parse("## hello")), "

hello

"); assertEquals(ren.render(par.parse("### hello")), "

hello

"); assertEquals(ren.render(par.parse("#### hello")), "

hello

"); assertEquals(ren.render(par.parse("##### hello")), "
hello
"); assertEquals(ren.render(par.parse("###### hello")), "
hello
"); }, }); Deno.test({ name: "should parse ATX header with closing sequence", fn: () => { const par = new MarkdownParser(); assertEquals(ren.render(par.parse("# #")), "

"); assertEquals(ren.render(par.parse("# hello #")), "

hello

"); assertEquals(ren.render(par.parse("# hello #########")), "

hello

"); assertEquals(ren.render(par.parse("# hello # ")), "

hello

"); assertEquals(ren.render(par.parse("###### hello #")), "
hello
"); }, }); Deno.test({ name: "should parse many headers with text", fn: () => { const par = new MarkdownParser(); const input = `\ # hello ## world ### this is #### my world!`; assertEquals( ren.render(par.parse(input)), "

hello

world

this is

my world!

", ); }, }); // Paragraph Deno.test({ name: "should parse paragraph", fn: () => { const par = new MarkdownParser(); assertEquals(ren.render(par.parse("hello")), "

hello

"); }, }); Deno.test({ name: "should parse paragraph with softbreak", fn: () => { const par = new MarkdownParser(); assertEquals( ren.render(par.parse(`\ hello world`)), "

hello world

", ); }, }); Deno.test({ name: "should parse many big paragraphs", fn: () => { const par = new MarkdownParser(); assertEquals( ren.render(par.parse(`\ hello world`)), "

hello world

", ); assertEquals( ren.render(par.parse(`\ Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.`)), "

Lorem Ipsum is simply dummy text of the printing and typesetting industry.

Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.

", ); }, }); // Link Deno.test({ name: "should parse link", fn: () => { const par = new MarkdownParser(); assertEquals( ren.render(par.parse("[]()")), '

', ); assertEquals( ren.render(par.parse("[hello]()")), '

hello

', ); assertEquals( ren.render(par.parse("[hello]()")), '

hello

', ); }, }); Deno.test({ name: "should parse link destination", fn: () => { const par = new MarkdownParser(); assertEquals( ren.render(par.parse("[](/hello)")), '

', ); assertEquals( ren.render(par.parse("[](/hello?key=value&key2=value2)")), '

', ); assertEquals( ren.render(par.parse("[hello](https://example.com)")), '

hello

', ); assertEquals( ren.render(par.parse("[hello](mailto:john@example.com)")), '

hello

', ); assertEquals( ren.render(par.parse("[](/привет)")), '

', ); assertEquals( ren.render(par.parse("[]()")), '

', ); assertEquals( ren.render(par.parse("[]()")), '

', ); }, }); Deno.test({ name: "should parse link title", fn: () => { const par = new MarkdownParser(); assertEquals( ren.render(par.parse("[](/hello 'hello')")), '

', ); assertEquals( ren.render(par.parse('[hello](/hello "world")')), '

hello

', ); assertEquals( ren.render(par.parse('[hello]( "hello world")')), '

hello

', ); }, }); // List Deno.test({ name: "should parse list with empty items", fn: () => { const par = new MarkdownParser(); assertEquals( ren.render(par.parse("-")), "", ); assertEquals( ren.render(par.parse("- ")), "", ); }, }); Deno.test({ name: "should parse list if line contains additional spaces", fn: () => { const expected = ""; const par = new MarkdownParser(); assertEquals(ren.render(par.parse(" - hello")), expected); assertEquals(ren.render(par.parse(" - hello")), expected); assertEquals(ren.render(par.parse(" - hello")), expected); }, }); Deno.test({ name: "should not display a single paragraph in the list", fn: () => { const par = new MarkdownParser(); assertEquals( ren.render(par.parse("- hello")), "", ); assertEquals( ren.render(par.parse(`\ - hello world`)), "", ); assertEquals( ren.render(par.parse(`\ - hello world`)), "", ); }, }); Deno.test({ name: "should parse many items in the list", fn: () => { const par = new MarkdownParser(); assertEquals( ren.render(par.parse(`\ - hello - world`)), "", ); }, }); // Doc Deno.test({ name: "should parse all document from file", fn: () => { const par = new MarkdownParser(); assertEquals( ren.render(par.parse(Deno.readTextFileSync("test_data/doc_01.md"))), "

What is Lorem Ipsum?

Lorem Ipsum is simply dummy text of the printing and typesetting industry.

", ); assertEquals( ren.render(par.parse(Deno.readTextFileSync("test_data/doc_02.md"))), "

What is Lorem Ipsum?

", ); }, });