Skip to content
New issue

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

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

Already on GitHub? # to your account

support json #89

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
45 changes: 44 additions & 1 deletion lib/update-linkstatus-obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@
* @param {Array} linkStatus - The existing link status to update.
* Each status is an object with properties `link`, `status`, `status_code`, `line_number`, `position`, `error_message`, `title`, and `children`.
*
* @param {Object} config - The configuration object.
*
* @returns {Array} The updated link status. Each status is an object with properties `link`, `status`, `status_code`, `line_number`, `position`, `error_message`, `title`, and `children`.
* The returned array is sorted by line number and start column in ascending order.
*/
'use strict'

function updateLinkStatusObj(astNodes, linkStatus) {
function updateLinkStatusObj(astNodes, linkStatus, config) {
const isJsonFile =
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually more ideally this wouldn't use the config for this. it should instead be determined per each file

config.fileExtensions && config.fileExtensions.includes('json')

if (isJsonFile) {
return handleJsonParsing(astNodes, linkStatus)
}

const updatedLinkStatus = [...linkStatus]
astNodes.forEach((node) => {
const existingLink = linkStatus.find((link) => link.link === node.url)
Expand Down Expand Up @@ -67,4 +76,38 @@ function updateLinkStatusObj(astNodes, linkStatus) {
return updatedLinkStatus
}

function handleJsonParsing(astNodes, linkStatus) {
const updatedLinkStatus = [...linkStatus]

astNodes.forEach((node) => {
if (typeof node === 'object' && node !== null) {
Object.values(node).forEach((value) => {
if (typeof value === 'string' && isValidUrl(value)) {
updatedLinkStatus.push({
link: value,
status: null,
status_code: null,
line_number: null,
position: null,
error_message: null,
title: null,
children: null,
})
}
})
}
})

return updatedLinkStatus
}

function isValidUrl(string) {
try {
new URL(string)
return true
} catch (_) {
return false
}
}

export { updateLinkStatusObj }
2 changes: 1 addition & 1 deletion linkspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export async function* linkspector(configFile, cmd) {
const linkStatus = await checkHyperlinks(uniqueLinks, config, file)

// Update linkStatusObjects with information about removed links
const updatedLinkStatus = updateLinkStatusObj(astNodes, linkStatus)
const updatedLinkStatus = updateLinkStatusObj(astNodes, linkStatus, config)

// Yield an object with the relative file path and its result
yield {
Expand Down
50 changes: 50 additions & 0 deletions test/json.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { describe, it, expect } from 'vitest'
import { updateLinkStatusObj } from '../lib/update-linkstatus-obj'

describe('updateLinkStatusObj', () => {
it('should handle JSON parsing when file extension is json', () => {
const astNodes = [
{
url: 'http://example.com',
position: {
start: { line: 1, column: 1 },
end: { line: 1, column: 20 },
},
},
{
url: 'http://example.org',
position: {
start: { line: 2, column: 1 },
end: { line: 2, column: 20 },
},
},
]
const linkStatus = []
const config = { fileExtensions: ['json'] }

const result = updateLinkStatusObj(astNodes, linkStatus, config)

expect(result).toEqual([
{
link: 'http://example.com',
status: null,
status_code: null,
line_number: null,
position: null,
error_message: null,
title: null,
children: null,
},
{
link: 'http://example.org',
status: null,
status_code: null,
line_number: null,
position: null,
error_message: null,
title: null,
children: null,
},
])
})
})