-
Notifications
You must be signed in to change notification settings - Fork 66.9k
Expand file tree
/
Copy pathlink-error-line-numbers.js
More file actions
154 lines (120 loc) · 4.4 KB
/
link-error-line-numbers.js
File metadata and controls
154 lines (120 loc) · 4.4 KB
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
import { describe, expect, test, beforeEach, afterEach } from 'vitest'
import { renderContent } from '#src/content-render/index.js'
import { TitleFromAutotitleError } from '#src/content-render/unified/rewrite-local-links.js'
describe('link error line numbers', () => {
let fs
let originalReadFileSync
let originalExistsSync
let mockContext
beforeEach(async () => {
// Set up file system mocking
fs = await import('fs')
originalReadFileSync = fs.default.readFileSync
originalExistsSync = fs.default.existsSync
fs.default.existsSync = () => true
// Set up basic mock context
mockContext = {
currentLanguage: 'en',
currentVersion: 'free-pro-team@latest',
pages: new Map(),
redirects: new Map(),
page: {
fullPath: '/fake/test-file.md',
},
}
})
afterEach(() => {
// Restore original functions
fs.default.readFileSync = originalReadFileSync
fs.default.existsSync = originalExistsSync
})
test('reports correct line numbers for broken AUTOTITLE links', async () => {
// Test content with frontmatter followed by content with a broken link
const template = `---
title: Test Page
version: 1.0
---
# Introduction
This is some content.
Here is a broken link: [AUTOTITLE](/nonexistent/page).
More content here.`
fs.default.readFileSync = () => template
try {
await renderContent(template, mockContext)
expect.fail('Expected TitleFromAutotitleError to be thrown')
} catch (error) {
expect(error).toBeInstanceOf(TitleFromAutotitleError)
// The broken link is on line 10 in the original file
// (3 lines of frontmatter + 1 blank line + 1 title + 1 blank + 1 content + 1 blank + 1 link line)
// The error message should reference the correct line number
expect(error.message).toContain('/nonexistent/page')
expect(error.message).toContain('could not be resolved')
expect(error.message).toContain('(Line: 10)')
}
})
test('reports correct line numbers with different frontmatter sizes', async () => {
mockContext.page.fullPath = '/fake/test-file-2.md'
// Test with more extensive frontmatter
const template = `---
title: Another Test Page
description: This is a test
author: Test Author
date: 2024-01-01
tags:
- test
- documentation
version: 2.0
---
# Main Title
Some introductory text here.
## Section
Content with a [AUTOTITLE](/another/nonexistent/page) link.`
fs.default.readFileSync = () => template
try {
await renderContent(template, mockContext)
expect.fail('Expected TitleFromAutotitleError to be thrown')
} catch (error) {
expect(error).toBeInstanceOf(TitleFromAutotitleError)
expect(error.message).toContain('/another/nonexistent/page')
expect(error.message).toContain('could not be resolved')
}
})
test('handles files without frontmatter correctly', async () => {
mockContext.page.fullPath = '/fake/no-frontmatter.md'
// Test content without frontmatter
const template = `# Simple Title
This is content without frontmatter.
Here is a broken link: [AUTOTITLE](/missing/page).`
fs.default.readFileSync = () => template
try {
await renderContent(template, mockContext)
expect.fail('Expected TitleFromAutotitleError to be thrown')
} catch (error) {
expect(error).toBeInstanceOf(TitleFromAutotitleError)
expect(error.message).toContain('/missing/page')
expect(error.message).toContain('could not be resolved')
}
})
test('error message format is improved', async () => {
mockContext.page.fullPath = '/fake/message-test.md'
const template = `---
title: Message Test
---
[AUTOTITLE](/test/broken/link)
`
fs.default.readFileSync = () => template
try {
await renderContent(template, mockContext)
expect.fail('Expected TitleFromAutotitleError to be thrown')
} catch (error) {
expect(error).toBeInstanceOf(TitleFromAutotitleError)
// Check that the new error message format is used
expect(error.message).toContain('could not be resolved in one or more versions')
expect(error.message).toContain('Make sure that this link can be reached from all versions')
expect(error.message).toContain('/test/broken/link')
// Check that the old error message format is NOT used
expect(error.message).not.toContain('Unable to find Page by')
expect(error.message).not.toContain('To fix it, look at')
}
})
})