|
@@ -1,19 +1,21 @@
|
|
|
import { fromMarkdown } from 'mdast-util-from-markdown';
|
|
import { fromMarkdown } from 'mdast-util-from-markdown';
|
|
|
import { toMarkdown } from 'mdast-util-to-markdown';
|
|
import { toMarkdown } from 'mdast-util-to-markdown';
|
|
|
-import test from 'tape';
|
|
|
|
|
import { removePosition } from 'unist-util-remove-position';
|
|
import { removePosition } from 'unist-util-remove-position';
|
|
|
|
|
+import { describe, it, expect } from 'vitest';
|
|
|
|
|
|
|
|
import { DirectiveType } from '../src/mdast-util-growi-directive/consts.js';
|
|
import { DirectiveType } from '../src/mdast-util-growi-directive/consts.js';
|
|
|
import { directiveFromMarkdown, directiveToMarkdown } from '../src/mdast-util-growi-directive/index.js';
|
|
import { directiveFromMarkdown, directiveToMarkdown } from '../src/mdast-util-growi-directive/index.js';
|
|
|
import { directive } from '../src/micromark-extension-growi-directive/index.js';
|
|
import { directive } from '../src/micromark-extension-growi-directive/index.js';
|
|
|
|
|
|
|
|
-test('markdown -> mdast', (t) => {
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- fromMarkdown('a $b[c](d) e.', {
|
|
|
|
|
- extensions: [directive()],
|
|
|
|
|
- mdastExtensions: [directiveFromMarkdown],
|
|
|
|
|
- }).children[0],
|
|
|
|
|
- {
|
|
|
|
|
|
|
+
|
|
|
|
|
+describe('markdown -> mdast', () => {
|
|
|
|
|
+ it('should support directives (text)', () => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ fromMarkdown('a $b[c](d) e.', {
|
|
|
|
|
+ extensions: [directive()],
|
|
|
|
|
+ mdastExtensions: [directiveFromMarkdown],
|
|
|
|
|
+ }).children[0],
|
|
|
|
|
+ ).toEqual({
|
|
|
type: 'paragraph',
|
|
type: 'paragraph',
|
|
|
children: [
|
|
children: [
|
|
|
{
|
|
{
|
|
@@ -56,16 +58,16 @@ test('markdown -> mdast', (t) => {
|
|
|
start: { line: 1, column: 1, offset: 0 },
|
|
start: { line: 1, column: 1, offset: 0 },
|
|
|
end: { line: 1, column: 14, offset: 13 },
|
|
end: { line: 1, column: 14, offset: 13 },
|
|
|
},
|
|
},
|
|
|
- },
|
|
|
|
|
- 'should support directives (text)',
|
|
|
|
|
- );
|
|
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- fromMarkdown('$a[b](c)', {
|
|
|
|
|
- extensions: [directive()],
|
|
|
|
|
- mdastExtensions: [directiveFromMarkdown],
|
|
|
|
|
- }).children[0],
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ it('should support directives (leaf)', () => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ fromMarkdown('$a[b](c)', {
|
|
|
|
|
+ extensions: [directive()],
|
|
|
|
|
+ mdastExtensions: [directiveFromMarkdown],
|
|
|
|
|
+ }).children[0],
|
|
|
|
|
+ ).toEqual({
|
|
|
type: DirectiveType.Leaf,
|
|
type: DirectiveType.Leaf,
|
|
|
name: 'a',
|
|
name: 'a',
|
|
|
attributes: { c: '' },
|
|
attributes: { c: '' },
|
|
@@ -83,19 +85,19 @@ test('markdown -> mdast', (t) => {
|
|
|
start: { line: 1, column: 1, offset: 0 },
|
|
start: { line: 1, column: 1, offset: 0 },
|
|
|
end: { line: 1, column: 9, offset: 8 },
|
|
end: { line: 1, column: 9, offset: 8 },
|
|
|
},
|
|
},
|
|
|
- },
|
|
|
|
|
- 'should support directives (leaf)',
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- removePosition(
|
|
|
|
|
- fromMarkdown('x $a[b *c*\nd]', {
|
|
|
|
|
- extensions: [directive()],
|
|
|
|
|
- mdastExtensions: [directiveFromMarkdown],
|
|
|
|
|
- }),
|
|
|
|
|
- true,
|
|
|
|
|
- ),
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should support content in a label', () => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ removePosition(
|
|
|
|
|
+ fromMarkdown('x $a[b *c*\nd]', {
|
|
|
|
|
+ extensions: [directive()],
|
|
|
|
|
+ mdastExtensions: [directiveFromMarkdown],
|
|
|
|
|
+ }),
|
|
|
|
|
+ true,
|
|
|
|
|
+ ),
|
|
|
|
|
+ ).toEqual({
|
|
|
type: 'root',
|
|
type: 'root',
|
|
|
children: [
|
|
children: [
|
|
|
{
|
|
{
|
|
@@ -115,27 +117,19 @@ test('markdown -> mdast', (t) => {
|
|
|
],
|
|
],
|
|
|
},
|
|
},
|
|
|
],
|
|
],
|
|
|
- },
|
|
|
|
|
- 'should support content in a label',
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- const hoge = removePosition(
|
|
|
|
|
- fromMarkdown('x $a(#b.c.d e=f g="h&i&unknown;j")', {
|
|
|
|
|
- extensions: [directive()],
|
|
|
|
|
- mdastExtensions: [directiveFromMarkdown],
|
|
|
|
|
- }),
|
|
|
|
|
- true,
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- removePosition(
|
|
|
|
|
- fromMarkdown('x $a(#b.c.d e=f g="h&i&unknown;j")', {
|
|
|
|
|
- extensions: [directive()],
|
|
|
|
|
- mdastExtensions: [directiveFromMarkdown],
|
|
|
|
|
- }),
|
|
|
|
|
- true,
|
|
|
|
|
- ),
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should support attributes', () => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ removePosition(
|
|
|
|
|
+ fromMarkdown('x $a(#b.c.d e=f g="h&i&unknown;j")', {
|
|
|
|
|
+ extensions: [directive()],
|
|
|
|
|
+ mdastExtensions: [directiveFromMarkdown],
|
|
|
|
|
+ }),
|
|
|
|
|
+ true,
|
|
|
|
|
+ ),
|
|
|
|
|
+ ).toEqual({
|
|
|
type: 'root',
|
|
type: 'root',
|
|
|
children: [
|
|
children: [
|
|
|
{
|
|
{
|
|
@@ -153,19 +147,19 @@ test('markdown -> mdast', (t) => {
|
|
|
],
|
|
],
|
|
|
},
|
|
},
|
|
|
],
|
|
],
|
|
|
- },
|
|
|
|
|
- 'should support attributes',
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- removePosition(
|
|
|
|
|
- fromMarkdown('$a(b\nc="d\ne")', {
|
|
|
|
|
- extensions: [directive()],
|
|
|
|
|
- mdastExtensions: [directiveFromMarkdown],
|
|
|
|
|
- }),
|
|
|
|
|
- true,
|
|
|
|
|
- ),
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should support EOLs in attributes', () => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ removePosition(
|
|
|
|
|
+ fromMarkdown('$a(b\nc="d\ne")', {
|
|
|
|
|
+ extensions: [directive()],
|
|
|
|
|
+ mdastExtensions: [directiveFromMarkdown],
|
|
|
|
|
+ }),
|
|
|
|
|
+ true,
|
|
|
|
|
+ ),
|
|
|
|
|
+ ).toEqual({
|
|
|
type: 'root',
|
|
type: 'root',
|
|
|
children: [
|
|
children: [
|
|
|
{
|
|
{
|
|
@@ -180,416 +174,369 @@ test('markdown -> mdast', (t) => {
|
|
|
],
|
|
],
|
|
|
},
|
|
},
|
|
|
],
|
|
],
|
|
|
- },
|
|
|
|
|
- 'should support EOLs in attributes',
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- t.end();
|
|
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
-test('mdast -> markdown', (t) => {
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- toMarkdown(
|
|
|
|
|
- {
|
|
|
|
|
- type: 'paragraph',
|
|
|
|
|
- children: [
|
|
|
|
|
- { type: 'text', value: 'a ' },
|
|
|
|
|
- // @ts-expect-error: `children`, `name` missing.
|
|
|
|
|
- { type: DirectiveType.Text },
|
|
|
|
|
- { type: 'text', value: ' b.' },
|
|
|
|
|
- ],
|
|
|
|
|
- },
|
|
|
|
|
- { extensions: [directiveToMarkdown] },
|
|
|
|
|
- ),
|
|
|
|
|
- 'a $ b.\n',
|
|
|
|
|
- 'should try to serialize a directive (text) w/o `name`',
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- toMarkdown(
|
|
|
|
|
- {
|
|
|
|
|
- type: 'paragraph',
|
|
|
|
|
- children: [
|
|
|
|
|
- { type: 'text', value: 'a ' },
|
|
|
|
|
- // @ts-expect-error: `children` missing.
|
|
|
|
|
- { type: DirectiveType.Text, name: 'b' },
|
|
|
|
|
- { type: 'text', value: ' c.' },
|
|
|
|
|
- ],
|
|
|
|
|
- },
|
|
|
|
|
- { extensions: [directiveToMarkdown] },
|
|
|
|
|
- ),
|
|
|
|
|
- 'a $b c.\n',
|
|
|
|
|
- 'should serialize a directive (text) w/ `name`',
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- toMarkdown(
|
|
|
|
|
- {
|
|
|
|
|
- type: 'paragraph',
|
|
|
|
|
- children: [
|
|
|
|
|
- { type: 'text', value: 'a ' },
|
|
|
|
|
- {
|
|
|
|
|
- type: DirectiveType.Text,
|
|
|
|
|
- name: 'b',
|
|
|
|
|
- children: [{ type: 'text', value: 'c' }],
|
|
|
|
|
- },
|
|
|
|
|
- { type: 'text', value: ' d.' },
|
|
|
|
|
- ],
|
|
|
|
|
- },
|
|
|
|
|
- { extensions: [directiveToMarkdown] },
|
|
|
|
|
- ),
|
|
|
|
|
- 'a $b[c] d.\n',
|
|
|
|
|
- 'should serialize a directive (text) w/ `children`',
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- toMarkdown(
|
|
|
|
|
- {
|
|
|
|
|
- type: 'paragraph',
|
|
|
|
|
- children: [
|
|
|
|
|
- { type: 'text', value: 'a ' },
|
|
|
|
|
- {
|
|
|
|
|
- type: DirectiveType.Text,
|
|
|
|
|
- name: 'b',
|
|
|
|
|
- children: [{ type: 'text', value: 'c[d]e' }],
|
|
|
|
|
- },
|
|
|
|
|
- { type: 'text', value: ' f.' },
|
|
|
|
|
- ],
|
|
|
|
|
- },
|
|
|
|
|
- { extensions: [directiveToMarkdown] },
|
|
|
|
|
- ),
|
|
|
|
|
- 'a $b[c\\[d\\]e] f.\n',
|
|
|
|
|
- 'should escape brackets in a directive (text) label',
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- toMarkdown(
|
|
|
|
|
- {
|
|
|
|
|
- type: 'paragraph',
|
|
|
|
|
- children: [
|
|
|
|
|
- { type: 'text', value: 'a ' },
|
|
|
|
|
- {
|
|
|
|
|
- type: DirectiveType.Text,
|
|
|
|
|
- name: 'b',
|
|
|
|
|
- children: [{ type: 'text', value: 'c\nd' }],
|
|
|
|
|
- },
|
|
|
|
|
- { type: 'text', value: ' e.' },
|
|
|
|
|
- ],
|
|
|
|
|
- },
|
|
|
|
|
- { extensions: [directiveToMarkdown] },
|
|
|
|
|
- ),
|
|
|
|
|
- 'a $b[c\nd] e.\n',
|
|
|
|
|
- 'should support EOLs in a directive (text) label',
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- toMarkdown(
|
|
|
|
|
- {
|
|
|
|
|
- type: 'paragraph',
|
|
|
|
|
- children: [
|
|
|
|
|
- { type: 'text', value: 'a ' },
|
|
|
|
|
- {
|
|
|
|
|
- type: DirectiveType.Text,
|
|
|
|
|
- name: 'b',
|
|
|
|
|
- // @ts-expect-error: should contain only `string`s
|
|
|
|
|
- attributes: {
|
|
|
|
|
- c: 'd', e: 'f', g: '', h: null, i: undefined, j: 2,
|
|
|
|
|
|
|
+describe('mdast -> markdown', () => {
|
|
|
|
|
+ it('should try to serialize a directive (text) w/o `name`', () => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ toMarkdown(
|
|
|
|
|
+ {
|
|
|
|
|
+ type: 'paragraph',
|
|
|
|
|
+ children: [
|
|
|
|
|
+ { type: 'text', value: 'a ' },
|
|
|
|
|
+ // @ts-expect-error: `children`, `name` missing.
|
|
|
|
|
+ { type: DirectiveType.Text },
|
|
|
|
|
+ { type: 'text', value: ' b.' },
|
|
|
|
|
+ ],
|
|
|
|
|
+ },
|
|
|
|
|
+ { extensions: [directiveToMarkdown] },
|
|
|
|
|
+ ),
|
|
|
|
|
+ ).toBe('a $ b.\n');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should serialize a directive (text) w/ `name`', () => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ toMarkdown(
|
|
|
|
|
+ {
|
|
|
|
|
+ type: 'paragraph',
|
|
|
|
|
+ children: [
|
|
|
|
|
+ { type: 'text', value: 'a ' },
|
|
|
|
|
+ // @ts-expect-error: `children` missing.
|
|
|
|
|
+ { type: DirectiveType.Text, name: 'b' },
|
|
|
|
|
+ { type: 'text', value: ' c.' },
|
|
|
|
|
+ ],
|
|
|
|
|
+ },
|
|
|
|
|
+ { extensions: [directiveToMarkdown] },
|
|
|
|
|
+ ),
|
|
|
|
|
+ ).toBe('a $b c.\n');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should serialize a directive (text) w/ `children`', () => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ toMarkdown(
|
|
|
|
|
+ {
|
|
|
|
|
+ type: 'paragraph',
|
|
|
|
|
+ children: [
|
|
|
|
|
+ { type: 'text', value: 'a ' },
|
|
|
|
|
+ {
|
|
|
|
|
+ type: DirectiveType.Text,
|
|
|
|
|
+ name: 'b',
|
|
|
|
|
+ children: [{ type: 'text', value: 'c' }],
|
|
|
},
|
|
},
|
|
|
- children: [],
|
|
|
|
|
- },
|
|
|
|
|
- { type: 'text', value: ' k.' },
|
|
|
|
|
- ],
|
|
|
|
|
- },
|
|
|
|
|
- { extensions: [directiveToMarkdown] },
|
|
|
|
|
- ),
|
|
|
|
|
- 'a $b(c="d" e="f" g j="2") k.\n',
|
|
|
|
|
- 'should serialize a directive (text) w/ `attributes`',
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- toMarkdown(
|
|
|
|
|
- {
|
|
|
|
|
- type: 'paragraph',
|
|
|
|
|
- children: [
|
|
|
|
|
- { type: 'text', value: 'a ' },
|
|
|
|
|
- {
|
|
|
|
|
- type: DirectiveType.Text,
|
|
|
|
|
- name: 'b',
|
|
|
|
|
- attributes: { '#d': '', '.a.b.c': '', key: 'value' },
|
|
|
|
|
- children: [],
|
|
|
|
|
- },
|
|
|
|
|
- { type: 'text', value: ' k.' },
|
|
|
|
|
- ],
|
|
|
|
|
- },
|
|
|
|
|
- { extensions: [directiveToMarkdown] },
|
|
|
|
|
- ),
|
|
|
|
|
- 'a $b(#d .a.b.c key="value") k.\n',
|
|
|
|
|
- 'should serialize a directive (text) w/ hash, dot notation attributes',
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- toMarkdown(
|
|
|
|
|
- {
|
|
|
|
|
- type: 'paragraph',
|
|
|
|
|
- children: [
|
|
|
|
|
- { type: 'text', value: 'a ' },
|
|
|
|
|
- {
|
|
|
|
|
- type: DirectiveType.Text,
|
|
|
|
|
- name: 'b',
|
|
|
|
|
- attributes: { x: 'y"\'\r\nz' },
|
|
|
|
|
- children: [],
|
|
|
|
|
- },
|
|
|
|
|
- { type: 'text', value: ' k.' },
|
|
|
|
|
- ],
|
|
|
|
|
- },
|
|
|
|
|
- { extensions: [directiveToMarkdown] },
|
|
|
|
|
- ),
|
|
|
|
|
- 'a $b(x="y"\'\r\nz") k.\n',
|
|
|
|
|
- 'should encode the quote in an attribute value (text)',
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- toMarkdown(
|
|
|
|
|
- {
|
|
|
|
|
- type: 'paragraph',
|
|
|
|
|
- children: [
|
|
|
|
|
- { type: 'text', value: 'a ' },
|
|
|
|
|
- {
|
|
|
|
|
- type: DirectiveType.Text,
|
|
|
|
|
- name: 'b',
|
|
|
|
|
- attributes: { x: 'y"\'\r\nz' },
|
|
|
|
|
- children: [],
|
|
|
|
|
- },
|
|
|
|
|
- { type: 'text', value: ' k.' },
|
|
|
|
|
- ],
|
|
|
|
|
- },
|
|
|
|
|
- { extensions: [directiveToMarkdown] },
|
|
|
|
|
- ),
|
|
|
|
|
- 'a $b(x="y"\'\r\nz") k.\n',
|
|
|
|
|
- 'should encode the quote in an attribute value (text)',
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- toMarkdown(
|
|
|
|
|
- {
|
|
|
|
|
- type: 'paragraph',
|
|
|
|
|
- children: [
|
|
|
|
|
- { type: 'text', value: 'a ' },
|
|
|
|
|
- {
|
|
|
|
|
- type: DirectiveType.Text,
|
|
|
|
|
- name: 'b',
|
|
|
|
|
- attributes: { id: 'c#d' },
|
|
|
|
|
- children: [],
|
|
|
|
|
- },
|
|
|
|
|
- { type: 'text', value: ' e.' },
|
|
|
|
|
- ],
|
|
|
|
|
- },
|
|
|
|
|
- { extensions: [directiveToMarkdown] },
|
|
|
|
|
- ),
|
|
|
|
|
- 'a $b(id="c#d") e.\n',
|
|
|
|
|
- 'should not use the `id` shortcut if impossible characters exist',
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- toMarkdown(
|
|
|
|
|
- {
|
|
|
|
|
- type: 'paragraph',
|
|
|
|
|
- children: [
|
|
|
|
|
- { type: 'text', value: 'a ' },
|
|
|
|
|
- {
|
|
|
|
|
- type: DirectiveType.Text,
|
|
|
|
|
- name: 'b',
|
|
|
|
|
- attributes: { 'c.d': '', 'e<f': '' },
|
|
|
|
|
- children: [],
|
|
|
|
|
- },
|
|
|
|
|
- { type: 'text', value: ' g.' },
|
|
|
|
|
- ],
|
|
|
|
|
- },
|
|
|
|
|
- { extensions: [directiveToMarkdown] },
|
|
|
|
|
- ),
|
|
|
|
|
- 'a $b(c.d e<f) g.\n',
|
|
|
|
|
- 'should not use the `class` shortcut if impossible characters exist',
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- toMarkdown(
|
|
|
|
|
- {
|
|
|
|
|
- type: 'paragraph',
|
|
|
|
|
- children: [
|
|
|
|
|
- { type: 'text', value: 'a ' },
|
|
|
|
|
- {
|
|
|
|
|
- type: DirectiveType.Text,
|
|
|
|
|
- name: 'b',
|
|
|
|
|
- attributes: {
|
|
|
|
|
- 'c.d': '', e: '', 'f<g': '', hij: '',
|
|
|
|
|
|
|
+ { type: 'text', value: ' d.' },
|
|
|
|
|
+ ],
|
|
|
|
|
+ },
|
|
|
|
|
+ { extensions: [directiveToMarkdown] },
|
|
|
|
|
+ ),
|
|
|
|
|
+ ).toBe('a $b[c] d.\n');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should escape brackets in a directive (text) label', () => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ toMarkdown(
|
|
|
|
|
+ {
|
|
|
|
|
+ type: 'paragraph',
|
|
|
|
|
+ children: [
|
|
|
|
|
+ { type: 'text', value: 'a ' },
|
|
|
|
|
+ {
|
|
|
|
|
+ type: DirectiveType.Text,
|
|
|
|
|
+ name: 'b',
|
|
|
|
|
+ children: [{ type: 'text', value: 'c[d]e' }],
|
|
|
},
|
|
},
|
|
|
- children: [],
|
|
|
|
|
- },
|
|
|
|
|
- { type: 'text', value: ' k.' },
|
|
|
|
|
- ],
|
|
|
|
|
- },
|
|
|
|
|
- { extensions: [directiveToMarkdown] },
|
|
|
|
|
- ),
|
|
|
|
|
- 'a $b(c.d e f<g hij) k.\n',
|
|
|
|
|
- 'should not use the `class` shortcut if impossible characters exist (but should use it for classes that don’t)',
|
|
|
|
|
- );
|
|
|
|
|
|
|
+ { type: 'text', value: ' f.' },
|
|
|
|
|
+ ],
|
|
|
|
|
+ },
|
|
|
|
|
+ { extensions: [directiveToMarkdown] },
|
|
|
|
|
+ ),
|
|
|
|
|
+ ).toBe('a $b[c\\[d\\]e] f.\n');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should support EOLs in a directive (text) label', () => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ toMarkdown(
|
|
|
|
|
+ {
|
|
|
|
|
+ type: 'paragraph',
|
|
|
|
|
+ children: [
|
|
|
|
|
+ { type: 'text', value: 'a ' },
|
|
|
|
|
+ {
|
|
|
|
|
+ type: DirectiveType.Text,
|
|
|
|
|
+ name: 'b',
|
|
|
|
|
+ children: [{ type: 'text', value: 'c\nd' }],
|
|
|
|
|
+ },
|
|
|
|
|
+ { type: 'text', value: ' e.' },
|
|
|
|
|
+ ],
|
|
|
|
|
+ },
|
|
|
|
|
+ { extensions: [directiveToMarkdown] },
|
|
|
|
|
+ ),
|
|
|
|
|
+ ).toBe('a $b[c\nd] e.\n');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should serialize a directive (text) w/ `attributes`', () => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ toMarkdown(
|
|
|
|
|
+ {
|
|
|
|
|
+ type: 'paragraph',
|
|
|
|
|
+ children: [
|
|
|
|
|
+ { type: 'text', value: 'a ' },
|
|
|
|
|
+ {
|
|
|
|
|
+ type: DirectiveType.Text,
|
|
|
|
|
+ name: 'b',
|
|
|
|
|
+ // @ts-expect-error: should contain only `string`s
|
|
|
|
|
+ attributes: {
|
|
|
|
|
+ c: 'd', e: 'f', g: '', h: null, i: undefined, j: 2,
|
|
|
|
|
+ },
|
|
|
|
|
+ children: [],
|
|
|
|
|
+ },
|
|
|
|
|
+ { type: 'text', value: ' k.' },
|
|
|
|
|
+ ],
|
|
|
|
|
+ },
|
|
|
|
|
+ { extensions: [directiveToMarkdown] },
|
|
|
|
|
+ ),
|
|
|
|
|
+ ).toBe('a $b(c="d" e="f" g j="2") k.\n');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should serialize a directive (text) w/ hash, dot notation attributes', () => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ toMarkdown(
|
|
|
|
|
+ {
|
|
|
|
|
+ type: 'paragraph',
|
|
|
|
|
+ children: [
|
|
|
|
|
+ { type: 'text', value: 'a ' },
|
|
|
|
|
+ {
|
|
|
|
|
+ type: DirectiveType.Text,
|
|
|
|
|
+ name: 'b',
|
|
|
|
|
+ attributes: { '#d': '', '.a.b.c': '', key: 'value' },
|
|
|
|
|
+ children: [],
|
|
|
|
|
+ },
|
|
|
|
|
+ { type: 'text', value: ' k.' },
|
|
|
|
|
+ ],
|
|
|
|
|
+ },
|
|
|
|
|
+ { extensions: [directiveToMarkdown] },
|
|
|
|
|
+ ),
|
|
|
|
|
+ ).toBe('a $b(#d .a.b.c key="value") k.\n');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should encode the quote in an attribute value (text)', () => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ toMarkdown(
|
|
|
|
|
+ {
|
|
|
|
|
+ type: 'paragraph',
|
|
|
|
|
+ children: [
|
|
|
|
|
+ { type: 'text', value: 'a ' },
|
|
|
|
|
+ {
|
|
|
|
|
+ type: DirectiveType.Text,
|
|
|
|
|
+ name: 'b',
|
|
|
|
|
+ attributes: { x: 'y"\'\r\nz' },
|
|
|
|
|
+ children: [],
|
|
|
|
|
+ },
|
|
|
|
|
+ { type: 'text', value: ' k.' },
|
|
|
|
|
+ ],
|
|
|
|
|
+ },
|
|
|
|
|
+ { extensions: [directiveToMarkdown] },
|
|
|
|
|
+ ),
|
|
|
|
|
+ ).toBe('a $b(x="y"\'\r\nz") k.\n');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should not use the `id` shortcut if impossible characters exist', () => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ toMarkdown(
|
|
|
|
|
+ {
|
|
|
|
|
+ type: 'paragraph',
|
|
|
|
|
+ children: [
|
|
|
|
|
+ { type: 'text', value: 'a ' },
|
|
|
|
|
+ {
|
|
|
|
|
+ type: DirectiveType.Text,
|
|
|
|
|
+ name: 'b',
|
|
|
|
|
+ attributes: { id: 'c#d' },
|
|
|
|
|
+ children: [],
|
|
|
|
|
+ },
|
|
|
|
|
+ { type: 'text', value: ' e.' },
|
|
|
|
|
+ ],
|
|
|
|
|
+ },
|
|
|
|
|
+ { extensions: [directiveToMarkdown] },
|
|
|
|
|
+ ),
|
|
|
|
|
+ ).toBe('a $b(id="c#d") e.\n');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should not use the `class` shortcut if impossible characters exist', () => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ toMarkdown(
|
|
|
|
|
+ {
|
|
|
|
|
+ type: 'paragraph',
|
|
|
|
|
+ children: [
|
|
|
|
|
+ { type: 'text', value: 'a ' },
|
|
|
|
|
+ {
|
|
|
|
|
+ type: DirectiveType.Text,
|
|
|
|
|
+ name: 'b',
|
|
|
|
|
+ attributes: { 'c.d': '', 'e<f': '' },
|
|
|
|
|
+ children: [],
|
|
|
|
|
+ },
|
|
|
|
|
+ { type: 'text', value: ' g.' },
|
|
|
|
|
+ ],
|
|
|
|
|
+ },
|
|
|
|
|
+ { extensions: [directiveToMarkdown] },
|
|
|
|
|
+ ),
|
|
|
|
|
+ ).toBe('a $b(c.d e<f) g.\n');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should not use the `class` shortcut if impossible characters exist (but should use it for classes that don\'t)', () => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ toMarkdown(
|
|
|
|
|
+ {
|
|
|
|
|
+ type: 'paragraph',
|
|
|
|
|
+ children: [
|
|
|
|
|
+ { type: 'text', value: 'a ' },
|
|
|
|
|
+ {
|
|
|
|
|
+ type: DirectiveType.Text,
|
|
|
|
|
+ name: 'b',
|
|
|
|
|
+ attributes: {
|
|
|
|
|
+ 'c.d': '', e: '', 'f<g': '', hij: '',
|
|
|
|
|
+ },
|
|
|
|
|
+ children: [],
|
|
|
|
|
+ },
|
|
|
|
|
+ { type: 'text', value: ' k.' },
|
|
|
|
|
+ ],
|
|
|
|
|
+ },
|
|
|
|
|
+ { extensions: [directiveToMarkdown] },
|
|
|
|
|
+ ),
|
|
|
|
|
+ ).toBe('a $b(c.d e f<g hij) k.\n');
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
|
|
+ it('should try to serialize a directive (leaf) w/o `name`', () => {
|
|
|
// @ts-expect-error: `children`, `name` missing.
|
|
// @ts-expect-error: `children`, `name` missing.
|
|
|
- toMarkdown({ type: DirectiveType.Leaf }, { extensions: [directiveToMarkdown] }),
|
|
|
|
|
- '$\n',
|
|
|
|
|
- 'should try to serialize a directive (leaf) w/o `name`',
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- toMarkdown(
|
|
|
|
|
- // @ts-expect-error: `children` missing.
|
|
|
|
|
- { type: DirectiveType.Leaf, name: 'a' },
|
|
|
|
|
- { extensions: [directiveToMarkdown] },
|
|
|
|
|
- ),
|
|
|
|
|
- '$a\n',
|
|
|
|
|
- 'should serialize a directive (leaf) w/ `name`',
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- toMarkdown(
|
|
|
|
|
- {
|
|
|
|
|
- type: DirectiveType.Leaf,
|
|
|
|
|
- name: 'a',
|
|
|
|
|
- children: [{ type: 'text', value: 'b' }],
|
|
|
|
|
- },
|
|
|
|
|
- { extensions: [directiveToMarkdown] },
|
|
|
|
|
- ),
|
|
|
|
|
- '$a[b]\n',
|
|
|
|
|
- 'should serialize a directive (leaf) w/ `children`',
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- toMarkdown(
|
|
|
|
|
- {
|
|
|
|
|
- type: DirectiveType.Leaf,
|
|
|
|
|
- name: 'a',
|
|
|
|
|
- children: [{ type: 'text', value: 'b' }],
|
|
|
|
|
- },
|
|
|
|
|
- { extensions: [directiveToMarkdown] },
|
|
|
|
|
- ),
|
|
|
|
|
- '$a[b]\n',
|
|
|
|
|
- 'should serialize a directive (leaf) w/ `children`',
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- toMarkdown(
|
|
|
|
|
- {
|
|
|
|
|
- type: DirectiveType.Leaf,
|
|
|
|
|
- name: 'a',
|
|
|
|
|
- children: [{ type: 'text', value: 'b\nc' }],
|
|
|
|
|
- },
|
|
|
|
|
- { extensions: [directiveToMarkdown] },
|
|
|
|
|
- ),
|
|
|
|
|
- '$a[b
c]\n',
|
|
|
|
|
- 'should serialize a directive (leaf) w/ EOLs in `children`',
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- toMarkdown(
|
|
|
|
|
- {
|
|
|
|
|
- type: DirectiveType.Leaf,
|
|
|
|
|
- name: 'a',
|
|
|
|
|
- attributes: { '#b': '', '.c.d': '', key: 'e\nf' },
|
|
|
|
|
- children: [],
|
|
|
|
|
- },
|
|
|
|
|
- { extensions: [directiveToMarkdown] },
|
|
|
|
|
- ),
|
|
|
|
|
- '$a(#b .c.d key="e
f")\n',
|
|
|
|
|
- 'should serialize a directive (leaf) w/ EOLs in `attributes`',
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- toMarkdown(
|
|
|
|
|
- {
|
|
|
|
|
- type: 'paragraph',
|
|
|
|
|
- children: [{ type: 'text', value: 'a$b' }],
|
|
|
|
|
- },
|
|
|
|
|
- { extensions: [directiveToMarkdown] },
|
|
|
|
|
- ),
|
|
|
|
|
- 'a\\$b\n',
|
|
|
|
|
- 'should escape a `:` in phrasing when followed by an alpha',
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- toMarkdown(
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ expect(
|
|
|
|
|
+ toMarkdown(
|
|
|
|
|
+ { type: DirectiveType.Leaf },
|
|
|
|
|
+ { extensions: [directiveToMarkdown] },
|
|
|
|
|
+ ),
|
|
|
|
|
+ ).toBe('$\n');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should serialize a directive (leaf) w/ `name`', () => {
|
|
|
|
|
+ // @ts-expect-error: `children` missing.
|
|
|
|
|
+ expect(
|
|
|
|
|
+ toMarkdown(
|
|
|
|
|
+ { type: DirectiveType.Leaf, name: 'a' },
|
|
|
|
|
+ { extensions: [directiveToMarkdown] },
|
|
|
|
|
+ ),
|
|
|
|
|
+ ).toBe('$a\n');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should serialize a directive (leaf) w/ `children`', () => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ toMarkdown(
|
|
|
|
|
+ {
|
|
|
|
|
+ type: DirectiveType.Leaf,
|
|
|
|
|
+ name: 'a',
|
|
|
|
|
+ children: [{ type: 'text', value: 'b' }],
|
|
|
|
|
+ },
|
|
|
|
|
+ { extensions: [directiveToMarkdown] },
|
|
|
|
|
+ ),
|
|
|
|
|
+ ).toBe('$a[b]\n');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should serialize a directive (leaf) w/ EOLs in `children`', () => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ toMarkdown(
|
|
|
|
|
+ {
|
|
|
|
|
+ type: DirectiveType.Leaf,
|
|
|
|
|
+ name: 'a',
|
|
|
|
|
+ children: [{ type: 'text', value: 'b\nc' }],
|
|
|
|
|
+ },
|
|
|
|
|
+ { extensions: [directiveToMarkdown] },
|
|
|
|
|
+ ),
|
|
|
|
|
+ ).toBe('$a[b
c]\n');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should serialize a directive (leaf) w/ EOLs in `attributes`', () => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ toMarkdown(
|
|
|
|
|
+ {
|
|
|
|
|
+ type: DirectiveType.Leaf,
|
|
|
|
|
+ name: 'a',
|
|
|
|
|
+ attributes: { '#b': '', '.c.d': '', key: 'e\nf' },
|
|
|
|
|
+ children: [],
|
|
|
|
|
+ },
|
|
|
|
|
+ { extensions: [directiveToMarkdown] },
|
|
|
|
|
+ ),
|
|
|
|
|
+ ).toEqual('$a(#b .c.d key="e
f")\n');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should escape a `:` in phrasing when followed by an alpha', () => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ toMarkdown(
|
|
|
|
|
+ {
|
|
|
|
|
+ type: 'paragraph',
|
|
|
|
|
+ children: [{ type: 'text', value: 'a$b' }],
|
|
|
|
|
+ },
|
|
|
|
|
+ { extensions: [directiveToMarkdown] },
|
|
|
|
|
+ ),
|
|
|
|
|
+ ).toEqual('a\\$b\n');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should not escape a `:` in phrasing when followed by a non-alpha', () => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ toMarkdown({
|
|
|
type: 'paragraph',
|
|
type: 'paragraph',
|
|
|
children: [{ type: 'text', value: 'a$9' }],
|
|
children: [{ type: 'text', value: 'a$9' }],
|
|
|
- },
|
|
|
|
|
- { extensions: [directiveToMarkdown] },
|
|
|
|
|
- ),
|
|
|
|
|
- 'a$9\n',
|
|
|
|
|
- 'should not escape a `:` in phrasing when followed by a non-alpha',
|
|
|
|
|
- );
|
|
|
|
|
|
|
+ }, { extensions: [directiveToMarkdown] }),
|
|
|
|
|
+ ).toBe('a$9\n');
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- toMarkdown(
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ it('should not escape a `:` in phrasing when preceded by a colon', () => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ toMarkdown({
|
|
|
type: 'paragraph',
|
|
type: 'paragraph',
|
|
|
children: [{ type: 'text', value: 'a$c' }],
|
|
children: [{ type: 'text', value: 'a$c' }],
|
|
|
- },
|
|
|
|
|
- { extensions: [directiveToMarkdown] },
|
|
|
|
|
- ),
|
|
|
|
|
- 'a\\$c\n',
|
|
|
|
|
- 'should not escape a `:` in phrasing when preceded by a colon',
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- toMarkdown(
|
|
|
|
|
- {
|
|
|
|
|
- type: 'paragraph',
|
|
|
|
|
- children: [{ type: 'text', value: '$\na' }],
|
|
|
|
|
- },
|
|
|
|
|
- { extensions: [directiveToMarkdown] },
|
|
|
|
|
- ),
|
|
|
|
|
- '$\na\n',
|
|
|
|
|
- 'should not escape a `:` at a break',
|
|
|
|
|
- );
|
|
|
|
|
|
|
+ }, { extensions: [directiveToMarkdown] }),
|
|
|
|
|
+ ).toBe('a\\$c\n');
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- toMarkdown(
|
|
|
|
|
- {
|
|
|
|
|
- type: 'paragraph',
|
|
|
|
|
- children: [{ type: 'text', value: '$a' }],
|
|
|
|
|
- },
|
|
|
|
|
- { extensions: [directiveToMarkdown] },
|
|
|
|
|
- ),
|
|
|
|
|
- '\\$a\n',
|
|
|
|
|
- 'should not escape a `:` at a break when followed by an alpha',
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- toMarkdown(
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ it('should not escape a `:` at a break', () => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ toMarkdown(
|
|
|
|
|
+ {
|
|
|
|
|
+ type: 'paragraph',
|
|
|
|
|
+ children: [{ type: 'text', value: '$\na' }],
|
|
|
|
|
+ },
|
|
|
|
|
+ { extensions: [directiveToMarkdown] },
|
|
|
|
|
+ ),
|
|
|
|
|
+ ).toBe('$\na\n');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should not escape a `:` at a break when followed by an alpha', () => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ toMarkdown(
|
|
|
|
|
+ {
|
|
|
|
|
+ type: 'paragraph',
|
|
|
|
|
+ children: [{ type: 'text', value: '$a' }],
|
|
|
|
|
+ },
|
|
|
|
|
+ { extensions: [directiveToMarkdown] },
|
|
|
|
|
+ ),
|
|
|
|
|
+ ).toBe('\\$a\n');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should escape a `:` at a break when followed by a colon', () => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ toMarkdown({
|
|
|
type: 'paragraph',
|
|
type: 'paragraph',
|
|
|
children: [{ type: 'text', value: '$\na' }],
|
|
children: [{ type: 'text', value: '$\na' }],
|
|
|
- },
|
|
|
|
|
- { extensions: [directiveToMarkdown] },
|
|
|
|
|
- ),
|
|
|
|
|
- '$\na\n',
|
|
|
|
|
- 'should escape a `:` at a break when followed by a colon',
|
|
|
|
|
- );
|
|
|
|
|
|
|
+ }, { extensions: [directiveToMarkdown] }),
|
|
|
|
|
+ ).toBe('$\na\n');
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
- t.deepEqual(
|
|
|
|
|
- toMarkdown(
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ it('should escape a `:` after a text directive', () => {
|
|
|
|
|
+ expect(
|
|
|
|
|
+ toMarkdown({
|
|
|
type: 'paragraph',
|
|
type: 'paragraph',
|
|
|
children: [
|
|
children: [
|
|
|
{ type: DirectiveType.Text, name: 'red', children: [] },
|
|
{ type: DirectiveType.Text, name: 'red', children: [] },
|
|
|
{ type: 'text', value: '$' },
|
|
{ type: 'text', value: '$' },
|
|
|
],
|
|
],
|
|
|
- },
|
|
|
|
|
- { extensions: [directiveToMarkdown] },
|
|
|
|
|
- ),
|
|
|
|
|
- '$red$\n',
|
|
|
|
|
- 'should escape a `:` after a text directive',
|
|
|
|
|
- );
|
|
|
|
|
|
|
+ }, { extensions: [directiveToMarkdown] }),
|
|
|
|
|
+ ).toBe('$red$\n');
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
- t.end();
|
|
|
|
|
});
|
|
});
|