import { html2Markdown } from '../src/'
import fs from 'fs'
import dedent from 'dedent'
import path from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
test('convert html', () => {
const html = dedent`
page title
title
`
const md = html2Markdown(html)
expect(typeof md).toBe('string')
expect(md).toBe('# title\n')
})
test('convert code', () => {
const html = `
code\ncode
`
const md = html2Markdown(html)
expect(typeof md).toBe('string')
expect(md).toBe('```\ncode\ncode\n```\n')
})
test('convert pre', () => {
const html = `pre
`
const md = html2Markdown(html)
expect(typeof md).toBe('string')
expect(md).toBe('```\npre\n```\n')
})
test('newlines', () => {
const html = `line1
line2
line3`
const md = html2Markdown(html)
expect(typeof md).toBe('string')
expect(md).toBe('line1 \nline2 \nline3\n')
})
test('convert github code block', () => {
const html = dedent`npm install mdast-squeeze-links
`
const md = html2Markdown(html)
expect(typeof md).toBe('string')
expect(md).toBe(dedent`\`\`\`shell
npm install mdast-squeeze-links
\`\`\`\n`)
})
test('convert gitlab code block', () => {
const html = dedent`mkdir build
cd build
cmake ..
make
make install
`
const md = html2Markdown(html)
expect(typeof md).toBe('string')
expect(md).toBe(dedent`\`\`\`shell
mkdir build
cd build
cmake ..
make
make install
\`\`\`\n`)
})
test('convert highlightjs code block', () => {
const html = dedent`<!doctype html>
<html>
<head>
<title>Title</title>
<style>
body {
font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif;
font-size: 100%;
}
#status {
white-space: pre;
text-overflow: ellipsis;
overflow: hidden;
max-width: 400px;
}
</style>
<script src="popup.js"></script>
</head>
<body>
<p id="text"></p>
</body>
</html>
`
const md = html2Markdown(html)
expect(typeof md).toBe('string')
expect(md).toBe(dedent`\`\`\`html
Title
\`\`\`\n`)
})
test('convert list', () => {
const html = dedent`
`
const md = html2Markdown(html)
expect(typeof md).toBe('string')
expect(md).toBe('- list 1\n- list 2\n')
})
test('file 1', () => {
const md = fs.readFileSync(__dirname + '/test1.html', 'utf-8')
const html = html2Markdown(md)
expect(typeof html).toBe('string')
expect(html).toBe(dedent`# TOC test
## Table of Contents
## 日本語セクション
あああ
## foo
\`\`\`
function hello () {
console.log('hi');
}
\`\`\`
## bar
\`\`\`
function hello () {
console.log('wow');
}
\`\`\`
## pika
\`\`\`
function hello () {
console.log('pika!');
}
\`\`\`\n`)
})
test('file 2', () => {
const html = fs.readFileSync(__dirname + '/test2.html', 'utf-8')
const md = html2Markdown(html)
expect(typeof md).toBe('string')
expect(md).toBe(dedent`hoge
# 2019-08-29
## 今日やったこと
- [x] Bash on Windowsを触る
- Windowsを最新版 1903 にアップデート
## 明日どうするか\n`)
})
test('relative link', () => {
const html = `
link
`
const md = html2Markdown(html, {
baseURI: 'https://www.craftz.dog/'
})
expect(typeof md).toBe('string')
expect(md).toBe(
'[link](https://www.craftz.dog/about) \n'
)
})
test('table', () => {
const html = dedent`
`
const md = html2Markdown(html)
expect(typeof md).toBe('string')
expect(md).toBe(dedent`| header 1 |
| -------- |
| column 1 |\n
`)
})
test('ignore comments', () => {
const html = `
hello
world
`
const md = html2Markdown(html)
expect(typeof md).toBe('string')
expect(md).toBe('hello world\n')
})
test('file 3', () => {
const html = fs.readFileSync(__dirname + '/test3.html', 'utf-8')
const md = html2Markdown(html)
expect(typeof md).toBe('string')
expect(md).toBe(
'Adds a user to the guild, provided you have a valid oauth2 access token for the user with the `guilds.join` scope. Returns a 201 Created with the [guild member](https://discord.com/developers/docs/resources/guild#guild-member-object) as the body, or 204 No Content if the user is already a member of the guild. Fires a [Guild Member Add](https://discord.com/developers/docs/topics/gateway-events#guild-member-add) Gateway event.\n'
)
})