diff --git a/__tests__/helpers.ts b/__tests__/helpers.ts new file mode 100644 index 0000000..ed5a50d --- /dev/null +++ b/__tests__/helpers.ts @@ -0,0 +1,2 @@ +const parser = require('../dist/lint-md-parser.cjs'); +export const { parseMd, revertMdAstNode, stringifyMdAst } = parser; diff --git a/__tests__/index.spec.ts b/__tests__/index.spec.ts index ebd0b8d..6b994a2 100644 --- a/__tests__/index.spec.ts +++ b/__tests__/index.spec.ts @@ -1,8 +1,7 @@ import * as fs from 'fs'; import * as path from 'path'; import { execFileSync } from 'child_process'; - -const { parseMd, revertMdAstNode, stringifyMdAst } = require('../dist/lint-md-parser.cjs'); +import { parseMd, revertMdAstNode, stringifyMdAst } from './helpers'; describe('test lint-md-parser', () => { const mdDemo = fs diff --git a/__tests__/plugins/directive.spec.ts b/__tests__/plugins/directive.spec.ts index 7fb00af..9f33eda 100644 --- a/__tests__/plugins/directive.spec.ts +++ b/__tests__/plugins/directive.spec.ts @@ -1,4 +1,4 @@ -const { parseMd } = require('../../dist/lint-md-parser.cjs'); +import { parseMd } from '../helpers'; describe('remark-directive plugin', () => { test('container directive :::note', () => { diff --git a/__tests__/plugins/gfm.spec.ts b/__tests__/plugins/gfm.spec.ts new file mode 100644 index 0000000..1916c7f --- /dev/null +++ b/__tests__/plugins/gfm.spec.ts @@ -0,0 +1,79 @@ +import { parseMd } from '../helpers'; + +describe('remark-gfm plugin', () => { + test('table with alignment', () => { + const root = parseMd('| a | b |\n|:---|---:|\n| 1 | 2 |'); + expect(root.children).toHaveLength(1); + const node = root.children[0]; + expect(node.type).toBe('table'); + if (node.type === 'table') { + expect(node.align).toEqual(['left', 'right']); + expect(node.children).toHaveLength(2); + } + }); + + test('task list with checked and unchecked items', () => { + const root = parseMd('- [x] done\n- [ ] todo'); + expect(root.children).toHaveLength(1); + const list = root.children[0]; + expect(list.type).toBe('list'); + if (list.type === 'list') { + expect(list.children).toHaveLength(2); + expect(list.children[0].type).toBe('listItem'); + expect(list.children[0].checked).toBe(true); + expect(list.children[1].type).toBe('listItem'); + expect(list.children[1].checked).toBe(false); + } + }); + + test('footnote definition', () => { + const root = parseMd('[^1]: note'); + expect(root.children).toHaveLength(1); + const node = root.children[0]; + expect(node.type).toBe('footnoteDefinition'); + if (node.type === 'footnoteDefinition') { + expect(node.identifier).toBe('1'); + } + }); + + test('footnote reference', () => { + const root = parseMd('text [^1]\n\n[^1]: note'); + const paragraph = root.children[0]; + expect(paragraph.type).toBe('paragraph'); + if (paragraph.type === 'paragraph') { + const ref = paragraph.children.find((child) => child.type === 'footnoteReference'); + expect(ref).toBeDefined(); + if (ref?.type === 'footnoteReference') { + expect(ref.identifier).toBe('1'); + } + } + }); + + test('autolink email', () => { + const root = parseMd(''); + const paragraph = root.children[0]; + expect(paragraph.type).toBe('paragraph'); + if (paragraph.type === 'paragraph') { + expect(paragraph.children).toHaveLength(1); + const link = paragraph.children[0]; + expect(link.type).toBe('link'); + if (link.type === 'link') { + expect(link.url).toBe('mailto:contact@example.com'); + } + } + }); + + test('bare URL in line', () => { + const root = parseMd('visit https://example.com'); + const paragraph = root.children[0]; + expect(paragraph.type).toBe('paragraph'); + if (paragraph.type === 'paragraph') { + expect(paragraph.children).toHaveLength(2); + const link = paragraph.children[1]; + expect(link.type).toBe('link'); + if (link.type === 'link') { + expect(link.url).toBe('https://example.com'); + } + } + }); +}); diff --git a/__tests__/plugins/math.spec.ts b/__tests__/plugins/math.spec.ts index fa2d32b..a0266e5 100644 --- a/__tests__/plugins/math.spec.ts +++ b/__tests__/plugins/math.spec.ts @@ -1,4 +1,4 @@ -const { parseMd } = require('../../dist/lint-md-parser.cjs'); +import { parseMd } from '../helpers'; describe('remark-math plugin', () => { test('block math with $$ on separate lines', () => {