diff --git a/bun.lockb b/bun.lockb index 648e70c..fb42b1f 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index f298b7b..d8b0620 100644 --- a/package.json +++ b/package.json @@ -30,12 +30,12 @@ "./dist" ], "scripts": { - "build": "rm -rf ./dist && tsc && bun build ./src/index.ts --outfile dist/index.js --target node" + "build": "bun test && rm -rf ./dist && tsc && bun build ./src/index.ts --outfile dist/index.js --target node", + "prepublishOnly": "bun run build" }, "devDependencies": { - "bun-types": "latest" - }, - "peerDependencies": { + "bun-types": "latest", + "markdown-it": "^13.0.2", "typescript": "^5.0.0" }, "engines": { diff --git a/tests/__snapshots__/base.test.ts.snap b/tests/__snapshots__/base.test.ts.snap new file mode 100644 index 0000000..9f6416c --- /dev/null +++ b/tests/__snapshots__/base.test.ts.snap @@ -0,0 +1,73 @@ +// Bun Snapshot v1, https://goo.gl/fbAQLP + +exports[`base no imports 1`] = ` +"

Some title

+ +
+

Some note

+
+ + + + + + + + + + + + + + + +
Column AColumn BColumn C
Somestuff
+" +`; + +exports[`base single import 1`] = ` +"

Here is a typescript snippet

+
export function sum(a: number, b: number): number {
+  return a + b
+}
+
+
+" +`; + +exports[`base multiple imports 1`] = ` +"

Here is a typescript snippet

+
export function sum(a: number, b: number): number {
+  return a + b
+}
+
+
+
+

This is some amazing quote

+
+" +`; + +exports[`base custom lines 1`] = ` +"

Bubble sort in go

+
func bubbleSort(arr []int) {
+	len := len(arr)
+	for i := 0; i < len-1; i++ {
+		for j := 0; j < len-i-1; j++ {
+			if arr[j] > arr[j+1] {
+				arr[j], arr[j+1] = arr[j+1], arr[j]
+			}
+		}
+	}
+	fmt.Println("\nAfter Bubble Sorting")
+	for _, val := range arr {
+		fmt.Println(val)
+	}
+}
+
+" +`; diff --git a/tests/__snapshots__/options.test.ts.snap b/tests/__snapshots__/options.test.ts.snap new file mode 100644 index 0000000..43a1400 --- /dev/null +++ b/tests/__snapshots__/options.test.ts.snap @@ -0,0 +1,40 @@ +// Bun Snapshot v1, https://goo.gl/fbAQLP + +exports[`options matcher custom matcher 1`] = ` +"

This is some amazing quote

+" +`; + +exports[`options matcher custom matcher 2`] = ` +"

This is some amazing quote

+" +`; + +exports[`options empty options 1`] = ` +"

Some title

+ +
+

Some note

+
+ + + + + + + + + + + + + + + +
Column AColumn BColumn C
Somestuff
+" +`; diff --git a/tests/__snapshots__/recursion.test.ts.snap b/tests/__snapshots__/recursion.test.ts.snap new file mode 100644 index 0000000..ce56d3b --- /dev/null +++ b/tests/__snapshots__/recursion.test.ts.snap @@ -0,0 +1,15 @@ +// Bun Snapshot v1, https://goo.gl/fbAQLP + +exports[`base import different files 1`] = ` +"

I am gonna import different stuff

+

Here is a typescript snippet

+
+

TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale.

+
+
export function sum(a: number, b: number): number {
+  return a + b
+}
+
+
+" +`; diff --git a/tests/base.test.ts b/tests/base.test.ts new file mode 100644 index 0000000..4dd15e0 --- /dev/null +++ b/tests/base.test.ts @@ -0,0 +1,24 @@ +import { describe, expect, test } from 'bun:test' +import { render } from './utils.ts' + +describe('base', () => { + test('no imports', async () => { + expect(await render('simple.md')).toMatchSnapshot() + }) + + test('single import', async () => { + expect(await render('whole.md')).toMatchSnapshot() + }) + + test('multiple imports', async () => { + expect(await render('multiple.md')).toMatchSnapshot() + }) + + test('fail on not found', async () => { + expect(() => render('notFound.md')).toThrow(/cannot locate file.*nirvana\.md/) + }) + + test('custom lines', async () => { + expect(await render('partial.md')).toMatchSnapshot() + }) +}) diff --git a/tests/fixtures/bubble-sort.go b/tests/fixtures/bubble-sort.go new file mode 100644 index 0000000..442ed2a --- /dev/null +++ b/tests/fixtures/bubble-sort.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +func main() { + sample := []int{3, 4, 5, 2, 1} + bubbleSort(sample) + sample = []int{3, 4, 5, 2, 1, 7, 8, -1, -3} + bubbleSort(sample) +} + +func bubbleSort(arr []int) { + len := len(arr) + for i := 0; i < len-1; i++ { + for j := 0; j < len-i-1; j++ { + if arr[j] > arr[j+1] { + arr[j], arr[j+1] = arr[j+1], arr[j] + } + } + } + fmt.Println("\nAfter Bubble Sorting") + for _, val := range arr { + fmt.Println(val) + } +} diff --git a/tests/fixtures/cycle-a.md b/tests/fixtures/cycle-a.md new file mode 100644 index 0000000..9d1fc97 --- /dev/null +++ b/tests/fixtures/cycle-a.md @@ -0,0 +1,3 @@ +# This should be impossible + +@import(tests/fixtures/cycle-b.md) diff --git a/tests/fixtures/cycle-b.md b/tests/fixtures/cycle-b.md new file mode 100644 index 0000000..926c5ed --- /dev/null +++ b/tests/fixtures/cycle-b.md @@ -0,0 +1,3 @@ +# This should be impossible + +@import(tests/fixtures/cycle-a.md) diff --git a/tests/fixtures/matcher-a.md b/tests/fixtures/matcher-a.md new file mode 100644 index 0000000..8353368 --- /dev/null +++ b/tests/fixtures/matcher-a.md @@ -0,0 +1 @@ +foo 'tests/fixtures/quote.txt' diff --git a/tests/fixtures/matcher-b.md b/tests/fixtures/matcher-b.md new file mode 100644 index 0000000..1a6c7d4 --- /dev/null +++ b/tests/fixtures/matcher-b.md @@ -0,0 +1 @@ +$tests/fixtures/quote.txt$ diff --git a/tests/fixtures/multiple.md b/tests/fixtures/multiple.md new file mode 100644 index 0000000..0cbff66 --- /dev/null +++ b/tests/fixtures/multiple.md @@ -0,0 +1,7 @@ +Here is a typescript snippet + +```ts +@import(tests/fixtures/sum.ts) +``` + +> @import(tests/fixtures/quote.txt) diff --git a/tests/fixtures/notFound.md b/tests/fixtures/notFound.md new file mode 100644 index 0000000..591516c --- /dev/null +++ b/tests/fixtures/notFound.md @@ -0,0 +1,3 @@ +# This needs to fail + +@import(some/path/to/nirvana.md) diff --git a/tests/fixtures/partial.md b/tests/fixtures/partial.md new file mode 100644 index 0000000..b74ce8f --- /dev/null +++ b/tests/fixtures/partial.md @@ -0,0 +1,5 @@ +# Bubble sort in go + +```go +@import(tests/fixtures/bubble-sort.go)[12-25] +``` diff --git a/tests/fixtures/quote.txt b/tests/fixtures/quote.txt new file mode 100644 index 0000000..0b67d6f --- /dev/null +++ b/tests/fixtures/quote.txt @@ -0,0 +1 @@ +This is some amazing quote diff --git a/tests/fixtures/rec-a.md b/tests/fixtures/rec-a.md new file mode 100644 index 0000000..641d305 --- /dev/null +++ b/tests/fixtures/rec-a.md @@ -0,0 +1,3 @@ +# I am gonna import different stuff + +@import(tests/fixtures/rec-b.md) diff --git a/tests/fixtures/rec-b.md b/tests/fixtures/rec-b.md new file mode 100644 index 0000000..b04978c --- /dev/null +++ b/tests/fixtures/rec-b.md @@ -0,0 +1,7 @@ +## Here is a typescript snippet + +> @import(tests/fixtures/rec-c.txt) + +```ts +@import(tests/fixtures/sum.ts) +``` diff --git a/tests/fixtures/rec-c.txt b/tests/fixtures/rec-c.txt new file mode 100644 index 0000000..81aac8c --- /dev/null +++ b/tests/fixtures/rec-c.txt @@ -0,0 +1 @@ +TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale. diff --git a/tests/fixtures/relative.md b/tests/fixtures/relative.md new file mode 100644 index 0000000..fc0eb32 --- /dev/null +++ b/tests/fixtures/relative.md @@ -0,0 +1,5 @@ +Here is a typescript snippet + +```ts +@import(sum.ts) +``` diff --git a/tests/fixtures/self-reference.md b/tests/fixtures/self-reference.md new file mode 100644 index 0000000..382a539 --- /dev/null +++ b/tests/fixtures/self-reference.md @@ -0,0 +1,3 @@ +# This should be impossible + +@import(tests/fixtures/self-reference.md) diff --git a/tests/fixtures/simple.md b/tests/fixtures/simple.md new file mode 100644 index 0000000..4f527f5 --- /dev/null +++ b/tests/fixtures/simple.md @@ -0,0 +1,11 @@ +# Some title + +- Some +- List +- Three + +> Some note + +| Column A | Column B | Column C | +| -------- | -------- | -------- | +| Some | `stuff` | | diff --git a/tests/fixtures/sum.ts b/tests/fixtures/sum.ts new file mode 100644 index 0000000..da1a570 --- /dev/null +++ b/tests/fixtures/sum.ts @@ -0,0 +1,3 @@ +export function sum(a: number, b: number): number { + return a + b +} diff --git a/tests/fixtures/whole.md b/tests/fixtures/whole.md new file mode 100644 index 0000000..cced0dc --- /dev/null +++ b/tests/fixtures/whole.md @@ -0,0 +1,5 @@ +Here is a typescript snippet + +```ts +@import(tests/fixtures/sum.ts) +``` diff --git a/tests/options.test.ts b/tests/options.test.ts new file mode 100644 index 0000000..499bf0c --- /dev/null +++ b/tests/options.test.ts @@ -0,0 +1,33 @@ +import { describe, expect, test } from 'bun:test' +import { render } from './utils.ts' + +describe('options', () => { + test('empty options', async () => { + expect(await render('simple.md', {})).toMatchSnapshot() + }) + + test('set root directory', async () => { + expect(await render('relative.md', { root: 'tests/fixtures' })) + }) + + describe('matcher', () => { + test('custom matcher', async () => { + const matcher = /foo '(?.+)'/g + expect(await render('matcher-a.md', { matcher })).toMatchSnapshot() + }) + test('custom matcher', async () => { + const matcher = /\$(?.+)\$/g + expect(await render('matcher-b.md', { matcher })).toMatchSnapshot() + }) + + test('fail for non global regexp', async () => { + const matcher = /foo '(?.+)'/ + expect(() => render('matcher-a.md', { matcher })).toThrow('RegExp must be global') + }) + + test('fail for missing "file" group', async () => { + const matcher = /foo '(.+)'/g + expect(() => render('matcher-a.md', { matcher })).toThrow('Regexp must expose a named group "file"') + }) + }) +}) diff --git a/tests/recursion.test.ts b/tests/recursion.test.ts new file mode 100644 index 0000000..7226821 --- /dev/null +++ b/tests/recursion.test.ts @@ -0,0 +1,17 @@ +import { describe, expect, test } from 'bun:test' +import { render } from './utils.ts' + +describe('base', () => { + test('should not be able to import itself', async () => { + expect(() => render('self-reference.md')).toThrow(/cycles are not allowed, already parsed.*self-reference\.md/) + }) + + test('should not be able to import cycles', async () => { + expect(() => render('cycle-a.md')).toThrow(/cycles are not allowed, already parsed.*cycle-b\.md/) + expect(() => render('cycle-b.md')).toThrow(/cycles are not allowed, already parsed.*cycle-a\.md/) + }) + + test('import different files', async () => { + expect(await render('rec-a.md')).toMatchSnapshot() + }) +}) diff --git a/tests/utils.ts b/tests/utils.ts new file mode 100644 index 0000000..8896576 --- /dev/null +++ b/tests/utils.ts @@ -0,0 +1,11 @@ +import { readFile } from 'fs/promises' +import { join } from 'path' +import MarkdownIt from 'markdown-it' +import { Options, importPlugin } from '../src/index.ts' + +export async function render(path: string, options: Options | undefined = undefined): Promise { + const filename = join('./tests/fixtures', path) + const input = await readFile(filename, 'utf-8') + const output = MarkdownIt().use(importPlugin, options).render(input) + return output +}