import { describe, expect, it } from '@jest/globals'; import clipboard from '@powerfulyang/clipboardy'; import { html2md } from './html2md'; describe('html2md', () => { it('should convert table to markdown', async () => { const part = ` name type description name string name version string version `; const html = ` ${part}
`; const partMd = await html2md(part); const result = `| name | type | description | | ------- | ------ | ----------- | | name | string | name | | version | string | version |`; expect(partMd).toBe(result); const md = await html2md(html); expect(md).toBe(result); }); it('should convert ul to markdown', async () => { const html = ` `; const md = await html2md(html); expect(md).toBe( `+ name + version`, ); }); it('should convert ol to markdown', async () => { const html = `
  1. name
  2. version
`; const md = await html2md(html); expect(md).toBe( `1. name 2. version`, ); }); it('should convert pre to markdown(pre+code+~)', async () => { const html = `
        
Copy code
console.log('hello world')
`; const md = await html2md(html); expect(md).toBe( `\`\`\`javascript console.log('hello world') \`\`\``, ); }); it('should convert pre to markdown(pre+~)', async () => { const html = `
pip install -i https://mirrors.cloud.tencent.com/pypi/simple <some-package>
`; const md = await html2md(html); expect(md).toBe(`\`\`\` pip install -i https://mirrors.cloud.tencent.com/pypi/simple \`\`\``); }); it('should convert p to markdown', async () => { const html = `

hello world

`; const md = await html2md(html); expect(md).toBe(`hello world`); }); it('should convert checkbox to markdown', async () => { const html = ` `; const md = await html2md(html); expect(md).toBe( `+ [x] name + [ ] version`, ); }); it('should convert katex to markdown', async () => { const html = `

x= (1)S(-1)^ {S} ×\\times (1.M) ×\\times 2e2^ {e}
e=E-1023

`; const md = await html2md(html); clipboard.writeSync(md); const res = clipboard.readSync(); expect(res).toBe(`x= $ (-1)^ {S} $ $ \\times $ (1.M) $ \\times $ $ 2^ {e} $\\ e=E-1023`); }); it('should convert katex to markdown', async () => { const html = `x=(1)S×(1.M)×2ex= (-1)^ {S} \\times (1.M) \\times 2^ {e}
e=E-1023`; const md = await html2md(html); expect(md).toBe(`$ x= (-1)^ {S} \\times (1.M) \\times 2^ {e} $\\ e=E-1023`); }); it('来自张鑫旭的网站代码块01', async () => { const html = `
<input name="code" autocomplete="username">
`; const md = await html2md(html); expect(md).toBe(`\`\`\` \`\`\``); }); it('来自张鑫旭的网站代码块02', async () => { const html = `
:-webkit-autofill { background-color: transparent; }
:autofill { background-color: transparent; }
`; const md = await html2md(html); expect(md).toBe(`\`\`\` :-webkit-autofill { background-color: transparent; } :autofill { background-color: transparent; } \`\`\``); }); it('convert image', async () => { const html = `image-20210804153000000`; const md = await html2md(html); expect(md).toBe( `![image-20210804153000000](http://localhost/assets/images/2021/08/20210804153000.png "img title")`, ); }); it('convert url', async () => { const html = `post title`; const md = await html2md(html); expect(md).toBe(`[post title](http://localhost/post/1)`); }); });