Skip to content

Commit

Permalink
support json
Browse files Browse the repository at this point in the history
  • Loading branch information
theskillwithin committed Oct 20, 2024
1 parent 95ca53e commit 58d9596
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
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 =
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,
},
])
})
})

0 comments on commit 58d9596

Please sign in to comment.