Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect md fix #74

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ program
process.exit(1)
}
} catch (error) {
console.error(kleur.red(`💥 Error: ${error.message}`))
console.error(kleur.red(`💥 Main error: ${error.message}`))
process.exit(1)
}
})
Expand Down
250 changes: 0 additions & 250 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,82 +5,6 @@ let cmd = {
json: true,
}

test('linkspector should check image links in Markdown file', async () => {
let hasErrorLinks = false
let currentFile = '' // Variable to store the current file name
let results = [] // Array to store the results if json is true

for await (const { file, result } of linkspector(
'./test/fixtures/markdown/image/imageTest.yml',
cmd
)) {
currentFile = file
for (const linkStatusObj of result) {
if (cmd.json) {
results.push({
file: currentFile,
link: linkStatusObj.link,
status_code: linkStatusObj.status_code,
line_number: linkStatusObj.line_number,
position: linkStatusObj.position,
status: linkStatusObj.status,
error_message: linkStatusObj.error_message,
})
}
if (linkStatusObj.status === 'error') {
hasErrorLinks = true
}
}
}

expect(hasErrorLinks).toBe(false)
expect(results.length).toBe(1)
expect(results[0].link).toBe(
'https://commons.wikimedia.org/wiki/Main_Page#/media/File:Praia_do_Ribeiro_do_Cavalo2.jpg'
)
expect(results[0].status).toBe('alive')
})

test('linkspector should check relative links in Markdown file', async () => {
let hasErrorLinks = false
let currentFile = '' // Variable to store the current file name
let results = [] // Array to store the results if json is true

for await (const { file, result } of linkspector(
'./test/fixtures/markdown/relative/.relativeTest.yml',
cmd
)) {
currentFile = file
for (const linkStatusObj of result) {
if (cmd.json) {
results.push({
file: currentFile,
link: linkStatusObj.link,
status_code: linkStatusObj.status_code,
line_number: linkStatusObj.line_number,
position: linkStatusObj.position,
status: linkStatusObj.status,
error_message: linkStatusObj.error_message,
})
}
if (linkStatusObj.status === 'error') {
hasErrorLinks = true
}
}
}

expect(hasErrorLinks).toBe(true)
expect(results.length).toBe(8)
expect(results[0].status).toBe('alive')
expect(results[1].status).toBe('alive')
expect(results[2].status).toBe('alive')
expect(results[3].status).toBe('alive')
expect(results[4].status).toBe('alive')
expect(results[5].status).toBe('alive')
expect(results[6].status).toBe('error')
expect(results[7].status).toBe('error')
})

test('linkspector should check top-level relative links in Markdown file', async () => {
let hasErrorLinks = false
let currentFile = '' // Variable to store the current file name
Expand Down Expand Up @@ -112,177 +36,3 @@ test('linkspector should check top-level relative links in Markdown file', async
expect(hasErrorLinks).toBe(false)
expect(results.length).toBe(21)
})

test('linkspector should add back the removed duplicates when returning the results', async () => {
let hasErrorLinks = false
let currentFile = '' // Variable to store the current file name
let results = [] // Array to store the results if json is true

for await (const { file, result } of linkspector(
'./test/fixtures/markdown/duplicates/duplicateTest.yml',
cmd
)) {
currentFile = file
for (const linkStatusObj of result) {
if (cmd.json) {
results.push({
file: currentFile,
link: linkStatusObj.link,
status_code: linkStatusObj.status_code,
line_number: linkStatusObj.line_number,
position: linkStatusObj.position,
status: linkStatusObj.status,
error_message: linkStatusObj.error_message,
})
}
if (linkStatusObj.status === 'error') {
hasErrorLinks = true
}
}
}

expect(hasErrorLinks).toBe(true)
expect(results.length).toBe(4)
expect(results[0].status).toBe('alive')
expect(results[1].status).toBe('error')
expect(results[2].status).toBe('alive')
expect(results[3].status).toBe('error')
})

test('linkspector should check hyperlinks in AsciiDoc files', async () => {
let hasErrorLinks = false
let currentFile = '' // Variable to store the current file name
let results = [] // Array to store the results if json is true

for await (const { file, result } of linkspector(
'./test/fixtures/asciidoc/hyperlinks/hyperlinksTest.yml',
cmd
)) {
currentFile = file
for (const linkStatusObj of result) {
if (cmd.json) {
results.push({
file: currentFile,
link: linkStatusObj.link,
status_code: linkStatusObj.status_code,
line_number: linkStatusObj.line_number,
position: linkStatusObj.position,
status: linkStatusObj.status,
error_message: linkStatusObj.error_message,
})
}
if (linkStatusObj.status === 'error') {
hasErrorLinks = true
}
}
}

expect(hasErrorLinks).toBe(true)
expect(results.length).toBe(4)
expect(results[0].status).toBe('error')
expect(results[1].status).toBe('alive')
expect(results[2].status).toBe('error')
expect(results[3].status).toBe('alive')
})

test('linkspector should check HTML encoded section links', async () => {
let hasErrorLinks = false
let currentFile = '' // Variable to store the current file name
let results = [] // Array to store the results if json is true

for await (const { file, result } of linkspector(
'./test/fixtures/markdown/decoded-sections/.decodedTest.yml',
cmd
)) {
currentFile = file
for (const linkStatusObj of result) {
if (cmd.json) {
results.push({
file: currentFile,
link: linkStatusObj.link,
status_code: linkStatusObj.status_code,
line_number: linkStatusObj.line_number,
position: linkStatusObj.position,
status: linkStatusObj.status,
error_message: linkStatusObj.error_message,
})
}
if (linkStatusObj.status === 'error') {
hasErrorLinks = true
}
}
}

expect(hasErrorLinks).toBe(false)
expect(results.length).toBe(4)
expect(results[0].status).toBe('alive')
expect(results[1].status).toBe('alive')
expect(results[2].status).toBe('alive')
expect(results[3].status).toBe('alive')
})

test('linkspector should check HTML encoded section links and include anchor names', async () => {
let hasErrorLinks = false
let currentFile = '' // Variable to store the current file name
let results = [] // Array to store the results if json is true

for await (const { file, result } of linkspector(
'./test/fixtures/markdown/with-html-anchors/.withHtmlAnchorsTest.yml',
cmd
)) {
currentFile = file
for (const linkStatusObj of result) {
if (cmd.json) {
results.push({
file: currentFile,
link: linkStatusObj.link,
status_code: linkStatusObj.status_code,
line_number: linkStatusObj.line_number,
position: linkStatusObj.position,
status: linkStatusObj.status,
error_message: linkStatusObj.error_message,
})
}
if (linkStatusObj.status === 'error') {
hasErrorLinks = true
}
}
}

// Test expectations for link checks
expect(hasErrorLinks).toBe(false)
expect(results.length).toBe(1)
expect(results[0].status).toBe('alive')
})

test('linkspector should check links to headings with inline bold, italic, code, link, and image elements', async () => {
let hasErrorLinks = false
let currentFile = '' // Variable to store the current file name
let results = [] // Array to store the results if json is true

for await (const { file, result } of linkspector(
'./test/fixtures/markdown/headings/headingsTest.yml',
cmd
)) {
currentFile = file
for (const linkStatusObj of result) {
if (cmd.json) {
results.push({
file: currentFile,
link: linkStatusObj.link,
status_code: linkStatusObj.status_code,
line_number: linkStatusObj.line_number,
position: linkStatusObj.position,
status: linkStatusObj.status,
error_message: linkStatusObj.error_message,
})
}
if (linkStatusObj.status === 'error') {
hasErrorLinks = true
}
}
}

expect(hasErrorLinks).toBe(false)
expect(results.length).toBe(8)
})
2 changes: 1 addition & 1 deletion lib/prepare-file-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function prepareFilesList(config) {
return relativeFiles.map((file) => resolve(process.cwd(), file)) // Convert back to absolute paths
} catch (err) {
// Handle any other errors that may occur
console.error(err.message)
console.error("Error: Couldn't prepare the list of files. ", err)
return []
}
}
Expand Down
16 changes: 15 additions & 1 deletion lib/update-linkstatus-obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,22 @@ function updateLinkStatusObj(astNodes, linkStatus) {
astNodes.forEach((node) => {
const existingLink = linkStatus.find((link) => link.link === node.url)
if (existingLink) {
if (!existingLink.position) {
console.error(
`ERROR: Markdown formatting error around link: ${existingLink.link}. Please check the file containing this link.`
)
existingLink.position = {
start: { line: 1, column: 1 },
end: { line: 1, column: 1 },
}
}

const existingPosition = existingLink.position
const nodePosition = node.position
const nodePosition = node.position || {
start: { line: 1, column: 1 },
end: { line: 1, column: 1 },
}

if (
existingPosition.start.line !== nodePosition.start.line ||
existingPosition.start.column !== nodePosition.start.column ||
Expand Down
4 changes: 2 additions & 2 deletions lib/validate-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ async function validateConfig(config) {
return true
} catch (err) {
if (err instanceof ValidationError) {
console.error(err.message, err.details)
console.error('Validation Error:', err.message)
throw err
} else if (err.message.includes('ENOENT: no such file or directory')) {
console.error('Error reading file:', err.message)
throw err
} else {
console.error('Error:', err.message)
console.error('Other Validation Error:', err.message)
throw err
}
return false
Expand Down
42 changes: 42 additions & 0 deletions test/fixtures/asciidoc/hyperlinks/asciidoc-hyperlinks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { expect, test } from 'vitest'
import { linkspector } from './linkspector.js'

let cmd = {
json: true,
}

test('linkspector should check hyperlinks in AsciiDoc files', async () => {
let hasErrorLinks = false
let currentFile = '' // Variable to store the current file name
let results = [] // Array to store the results if json is true

for await (const { file, result } of linkspector(
'./test/fixtures/asciidoc/hyperlinks/hyperlinksTest.yml',
cmd
)) {
currentFile = file
for (const linkStatusObj of result) {
if (cmd.json) {
results.push({
file: currentFile,
link: linkStatusObj.link,
status_code: linkStatusObj.status_code,
line_number: linkStatusObj.line_number,
position: linkStatusObj.position,
status: linkStatusObj.status,
error_message: linkStatusObj.error_message,
})
}
if (linkStatusObj.status === 'error') {
hasErrorLinks = true
}
}
}

expect(hasErrorLinks).toBe(true)
expect(results.length).toBe(4)
expect(results[0].status).toBe('error')
expect(results[1].status).toBe('alive')
expect(results[2].status).toBe('error')
expect(results[3].status).toBe('alive')
})
Loading