-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmarkdown.spec.ts
305 lines (256 loc) · 11.4 KB
/
markdown.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import { JSDOM } from 'jsdom'
import { readability, markdown, turndown, getDocument, detectLanguage, TurndownOptions, MarkdownOptions } from './src'
import { describe, it, expect, vi, beforeEach } from 'vitest'
import TurndownService from 'turndown'
describe('readability', () => {
let mockDocument: Document
beforeEach(() => {
const dom = new JSDOM()
global.document = dom.window.document
mockDocument = document.implementation.createHTMLDocument()
mockDocument.body.innerHTML = `
<h1>Test Title</h1>
<p>Test content</p>
<img src="lazy.jpg" data-src="actual.jpg" class="lazy">
`
})
it('expect readability work', async () => {
const doc = await getDocument('https://juejin.cn/post/6922229465468633095')
const r = await readability(doc)
expect(r?.title).to.eq('山月最近的面试总结提供一个较少提过的方法,使用 grid,它是做二维布局的,但是只有一个子元素时,一维布局与二维布局就一 - 掘金')
expect(r?.byline).to.eq('程序员山月')
})
it('should handle lazy-loaded images', async () => {
const result = await readability(mockDocument)
expect(result?.content).to.include('src="actual.jpg"')
})
it('should extract byline from meta tag', async () => {
const metaTag = mockDocument.createElement('meta')
metaTag.setAttribute('itemprop', 'name')
metaTag.setAttribute('content', 'Test Author')
mockDocument.head.appendChild(metaTag)
const result = await readability(mockDocument)
expect(result?.byline).to.eq('Test Author')
})
it('should return full HTML content when platform skip is true', async () => {
vi.mock('./src/platform/index', () => ({
platforms: [{
filter: () => true,
skip: true,
processDocument: vi.fn()
}]
}))
const result = await readability(mockDocument)
expect(result?.content).to.include('<h1>Test Title</h1>')
expect(result?.content).to.include('<p>Test content</p>')
})
})
describe('markdown', function () {
it('expect markdown work', async () => {
const r = await markdown('https://juejin.cn/post/6922229465468633095')
expect(r?.markdown).to.length.gt(100)
})
it('expect read markdown from html work', async () => {
const r = turndown('<h1>hello, world</h1>')
expect(r).to.eq('# hello, world')
})
it('expect read markdown from node weekly', async () => {
const r = await markdown('https://nodeweekly.com/issues/522')
expect(r?.title).to.eq('Node Weekly Issue 522: February 27, 2024')
})
it('expect markdown function to return correct URL', async () => {
const testUrl = 'https://example.com/test-page'
const r = await markdown(testUrl)
expect(r).not.to.be.null
expect(r?.url).to.eq(testUrl)
})
it('should use custom TurndownOptions when provided', async () => {
const testUrl = 'https://example.com/test-page'
const mockHtml = `
<html>
<body>
<h1>Test Title</h1>
<ul>
<li>List item 1</li>
<li>List item 2</li>
</ul>
</body>
</html>
`
const customFetcher = vi.fn().mockResolvedValue(mockHtml)
const customOptions: MarkdownOptions = {
headingStyle: 'setext',
bulletListMarker: '*',
fetcher: customFetcher
}
const r = await markdown(testUrl, customOptions)
expect(customFetcher).toHaveBeenCalledWith(testUrl)
expect(r).not.to.be.null
expect(r?.markdown).to.include('Test Title\n=========')
expect(r?.markdown).to.include('* List item 1')
expect(r?.markdown).to.include('* List item 2')
})
it('should use underscore as bullet list marker when specified', async () => {
const testUrl = 'https://example.com/test-page'
const mockHtml = `
<html>
<body>
<h1>Test Title</h1>
<ul>
<li>List item 1</li>
<li>List item 2</li>
</ul>
</body>
</html>
`
const customFetcher = vi.fn().mockResolvedValue(mockHtml)
const customOptions: MarkdownOptions = {
bulletListMarker: '+',
fetcher: customFetcher
}
const r = await markdown(testUrl, customOptions)
expect(customFetcher).toHaveBeenCalledWith(testUrl)
expect(r).not.to.be.null
expect(r?.markdown).to.include('+ List item 1')
expect(r?.markdown).to.include('+ List item 2')
})
it('should include length property in MarkdownContent', async () => {
const testUrl = 'https://example.com/test-page'
const r = await markdown(testUrl)
expect(r).not.to.be.null
expect(r?.length).to.be.a('number')
expect(r?.length).to.equal(r?.markdown.length)
})
it('should use custom headers when provided', async () => {
const testUrl = 'https://httpbin.org/headers'
const customHeaders = { 'X-Custom-Header': 'Test' }
const r = await markdown(testUrl, { headers: customHeaders })
expect(r).not.to.be.null
expect(r?.markdown).to.include('X-Custom-Header')
expect(r?.markdown).to.include('Test')
})
it('should use custom fetcher when provided', async () => {
const testUrl = 'https://example.com/test-page'
const mockHtml = '<html><body><h1>Custom Fetcher Test</h1></body></html>'
const customFetcher = vi.fn().mockResolvedValue(mockHtml)
const r = await markdown(testUrl, { fetcher: customFetcher })
expect(customFetcher).toHaveBeenCalledWith(testUrl)
expect(r).not.to.be.null
expect(r?.markdown).to.include('# Custom Fetcher Test')
})
})
describe('turndown', () => {
describe('fencedCodeBlockWithoutCodeElement', () => {
it('should convert pre tag without code tag to markdown code block', async () => {
const html = '<pre>console.log("Hello, world!");</pre>'
const result = turndown(html)
expect(result).to.eq('```\nconsole.log("Hello, world!");\n```')
})
it('should detect language from pre tag class', async () => {
const html = '<pre class="language-javascript">const greeting = "Hello, world!";</pre>'
const result = turndown(html)
expect(result).to.eq('```javascript\nconst greeting = "Hello, world!";\n```')
})
it('should detect language from pre tag data attribute', async () => {
const html = '<pre data-lang="python">print("Hello, world!")</pre>'
const result = turndown(html)
expect(result).to.eq('```python\nprint("Hello, world!")\n```')
})
it('should detect language from parent element class', async () => {
const html = '<div class="highlight-ruby"><pre>puts "Hello, world!"</pre></div>'
const result = turndown(html)
expect(result).to.eq('```ruby\nputs "Hello, world!"\n```')
})
it('should handle pre tag with nested elements', async () => {
const html = '<pre><span class="keyword">const</span> x = 5;</pre>'
const result = turndown(html)
expect(result).to.eq('```\nconst x = 5;\n```')
})
it('should preserve line breaks in pre tag content', async () => {
const html = '<pre>line1\nline2\nline3</pre>'
const result = turndown(html)
expect(result).to.eq('```\nline1\nline2\nline3\n```')
})
it('should handle pre tag with br elements', async () => {
const html = '<pre>line1<br>line2<br />line3</pre>'
const result = turndown(html)
expect(result).to.eq('```\nline1\nline2\nline3\n```')
})
// New test case
it('should handle complex pre tag with nested elements and classes', async () => {
const html = `<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto"><pre><span class="pl-k">import</span> <span class="pl-kos">{</span> <span class="pl-s1">sum</span> <span class="pl-kos">}</span> <span class="pl-k">from</span> <span class="pl-s">'midash'</span>
<span class="pl-en">sum</span><span class="pl-kos">(</span><span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">,</span> <span class="pl-c1">3</span><span class="pl-kos">,</span> <span class="pl-c1">5</span><span class="pl-kos">,</span> <span class="pl-c1">7</span><span class="pl-kos">,</span> <span class="pl-c1">9</span><span class="pl-kos">]</span><span class="pl-kos">)</span></pre></div>`
const result = turndown(html)
expect(result).to.eq("```js\nimport { sum } from 'midash'\n\nsum([1, 3, 5, 7, 9])\n```")
})
})
describe('options', () => {
it('should use custom options when provided', () => {
const html = '<h1>Hello</h1><em>World</em>'
const options: TurndownOptions = {
headingStyle: 'setext',
emDelimiter: '_'
}
const result = turndown(html, options)
expect(result).to.eq('Hello\n=====\n\n_World_')
})
it('should override default options', () => {
const html = '<h2>Test</h2><ul><li>Item 1</li><li>Item 2</li></ul>'
const options: TurndownOptions = {
headingStyle: 'setext',
bulletListMarker: '*'
}
const result = turndown(html, options)
expect(result).to.eq('Test\n----\n\n* Item 1\n* Item 2')
})
it('should use default options when not provided', () => {
const html = '<h2>Default</h2><ul><li>Item 1</li><li>Item 2</li></ul>'
const result = turndown(html)
expect(result).to.eq('## Default\n\n+ Item 1\n+ Item 2')
})
})
})
describe('getDocument', () => {
it('should fetch and parse HTML document', async () => {
const doc = await getDocument('https://example.com')
expect(doc.querySelector('title')?.textContent).to.eq('Example Domain')
})
it('should use custom headers when provided', async () => {
const customHeaders = { 'X-Custom-Header': 'Test' }
const doc = await getDocument('https://httpbin.org/headers', { headers: customHeaders })
const responseBody = JSON.parse(doc.body.textContent || '{}')
expect(responseBody.headers['X-Custom-Header']).to.eq('Test')
})
it('should use default User-Agent when no headers provided', async () => {
const doc = await getDocument('https://httpbin.org/user-agent')
const responseBody = JSON.parse(doc.body.textContent || '{}')
expect(responseBody['user-agent']).to.include('Googlebot')
})
it('should use custom fetcher when provided', async () => {
const mockHtml = '<html><body><h1>Custom Fetcher Test</h1></body></html>'
const customFetcher = vi.fn().mockResolvedValue(mockHtml)
const doc = await getDocument('https://example.com', { fetcher: customFetcher })
expect(customFetcher).toHaveBeenCalledWith('https://example.com')
expect(doc.querySelector('h1')?.textContent).to.eq('Custom Fetcher Test')
})
})
describe('detectLanguage', () => {
it('should detect language from class name with language- prefix', () => {
expect(detectLanguage('language-javascript')).to.eq('javascript')
expect(detectLanguage('language-python')).to.eq('python')
})
it('should detect language from class name with lang- prefix', () => {
expect(detectLanguage('lang-css')).to.eq('css')
expect(detectLanguage('lang-ruby')).to.eq('ruby')
})
it('should detect language from class name without prefix', () => {
expect(detectLanguage('javascript')).to.eq('javascript')
expect(detectLanguage('python')).to.eq('python')
})
it('should return empty string for unrecognized language', () => {
expect(detectLanguage('unknown-language')).to.eq('')
})
it('should handle multiple classes and return the first recognized language', () => {
expect(detectLanguage('foo bar language-typescript javascript')).to.eq('typescript')
})
})