Skip to content

Commit ede4f09

Browse files
authored
chore(core|utils): unit test for functions, fixed minor issues (#12)
1 parent c48ab56 commit ede4f09

17 files changed

Lines changed: 149 additions & 19 deletions

File tree

.github/workflows/ci.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,27 @@ jobs:
4545
run: |
4646
pnpm run build
4747
48+
unit-test:
49+
name: Unit Test
50+
runs-on: ubuntu-latest
51+
steps:
52+
- uses: actions/checkout@v4
53+
- uses: pnpm/action-setup@v3
54+
- uses: actions/setup-node@v4
55+
with:
56+
node-version: lts/*
57+
cache: pnpm
58+
59+
- name: Install
60+
run: pnpm install
61+
62+
- name: Build
63+
run: |
64+
pnpm run build
65+
66+
- name: Unit Test
67+
run: pnpm run test:run
68+
4869
typecheck:
4970
name: Type Check
5071
runs-on: ubuntu-latest

examples/native-node/src/md.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@ import { readFile } from 'node:fs/promises'
22
import { renderMarkdownString } from '@velin-dev/core'
33
import { ref } from 'vue'
44

5-
(async () => {
5+
async function main() {
66
const markdownString = await readFile('./src/assets/markdown.md', 'utf-8')
7-
// const composableString = await fs.readFile('examples/assets/Composable.md', 'utf-8')
8-
97
const result1 = await renderMarkdownString(markdownString, { language: ref('TypeScript') })
108

11-
// const result2 = await processMarkdown(composableString, {
12-
// markdown: ref(result1),
13-
// })
14-
159
// eslint-disable-next-line no-console
1610
console.log(result1)
17-
})()
11+
}
12+
13+
main()

examples/native-node/src/sfc.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import { readFile } from 'node:fs/promises'
22
import { renderSFCString } from '@velin-dev/core'
33
import { ref } from 'vue'
44

5-
(async () => {
5+
async function main() {
66
const source = await readFile('./src/assets/MyComponent.vue', 'utf-8')
7-
87
const html = await renderSFCString(source, { language: ref('TypeScript') })
8+
99
// eslint-disable-next-line no-console
1010
console.log(html)
11-
})()
11+
}
12+
13+
main()

packages/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"@vue/shared": "^3.5.13",
6666
"defu": "^6.1.4",
6767
"error-stack-parser": "^2.1.4",
68+
"hast-util-from-html": "^2.0.3",
6869
"path-browserify-esm": "^1.0.6",
6970
"vue": "^3.5.13"
7071
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { readFile } from 'node:fs/promises'
2+
import { dirname, join } from 'node:path'
3+
import { fileURLToPath } from 'node:url'
4+
import { describe, expect, it } from 'vitest'
5+
6+
import { renderMarkdownString } from './markdown'
7+
8+
describe('renderMarkdownString', async () => {
9+
it('should be able to render simple SFC', async () => {
10+
const content = await readFile(join(dirname(fileURLToPath(import.meta.url)), 'testdata', 'simple.velin.md'), 'utf-8')
11+
const result = await renderMarkdownString(content)
12+
expect(result).toBeDefined()
13+
expect(result).not.toBe('')
14+
expect(result).toBe('# Hello, world!\n')
15+
})
16+
17+
it('should be able to render script setup SFC', async () => {
18+
const content = await readFile(join(dirname(fileURLToPath(import.meta.url)), 'testdata', 'script-setup.velin.md'), 'utf-8')
19+
const result = await renderMarkdownString(content)
20+
expect(result).toBeDefined()
21+
expect(result).not.toBe('')
22+
expect(result).toBe('Count: 0\n')
23+
})
24+
})

packages/core/src/render-node/markdown.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ export async function renderMarkdownString<RawProps = any>(
1515

1616
const { remainingHTML, scriptContent } = scriptFrom(html)
1717
const sfcString = createSFC(remainingHTML, scriptContent)
18-
const renderedHTML = await renderSFC(sfcString, data, basePath)
1918

19+
const renderedHTML = await renderSFC(sfcString, data, basePath)
2020
const markdownResult = await toMarkdown(renderedHTML)
21+
2122
return markdownResult
2223
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { readFile } from 'node:fs/promises'
2+
import { dirname, join } from 'node:path'
3+
import { fileURLToPath } from 'node:url'
4+
import { describe, expect, it } from 'vitest'
5+
6+
import { renderSFCString } from './sfc'
7+
8+
describe('renderMarkdownString', async () => {
9+
it('should be able to render simple SFC', async () => {
10+
const content = await readFile(join(dirname(fileURLToPath(import.meta.url)), 'testdata', 'simple.velin.vue'), 'utf-8')
11+
const result = await renderSFCString(content)
12+
expect(result).toBeDefined()
13+
expect(result).not.toBe('')
14+
expect(result).toBe('# Hello, world!\n')
15+
})
16+
17+
it('should be able to render script setup SFC', async () => {
18+
const content = await readFile(join(dirname(fileURLToPath(import.meta.url)), 'testdata', 'script-setup.velin.vue'), 'utf-8')
19+
const result = await renderSFCString(content)
20+
expect(result).toBeDefined()
21+
expect(result).not.toBe('')
22+
expect(result).toBe('# Count: 0\n')
23+
})
24+
})

packages/core/src/render-node/sfc.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { toMarkdown } from '@velin-dev/utils/to-md'
77
import { compileScript, compileTemplate, parse } from '@vue/compiler-sfc'
88
import defu from 'defu'
99
import ErrorStackParser from 'error-stack-parser'
10+
import { fromHtml } from 'hast-util-from-html'
1011
import path from 'path-browserify-esm'
1112

1213
import { onlyRender } from '../render-shared'
@@ -94,6 +95,12 @@ export async function renderSFCString<RawProps = any>(
9495
data?: InputProps<RawProps>,
9596
basePath?: string,
9697
): Promise<string> {
98+
const hastRoot = fromHtml(source, { fragment: true })
99+
const hasScript = hastRoot.children.some(node => node.type === 'element' && node.tagName === 'script')
100+
if (!hasScript) {
101+
source = `${source}\n<script setup>/* EMPTY */</script>`
102+
}
103+
97104
const html = await renderSFC(source, data, basePath)
98105
return toMarkdown(html)
99106
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script setup>
2+
import { ref } from 'vue'
3+
4+
const count = ref(0)
5+
</script>
6+
7+
Count: {{ count }}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script setup>
2+
import { ref } from 'vue'
3+
4+
const count = ref(0)
5+
</script>
6+
7+
<template>
8+
<div>
9+
<h1>Count: {{ count }}</h1>
10+
</div>
11+
</template>

0 commit comments

Comments
 (0)