From 58d95961378bb0324ed37505ea6685b7ca103a34 Mon Sep 17 00:00:00 2001 From: Austin Peterson Date: Sun, 20 Oct 2024 01:14:23 -0600 Subject: [PATCH] support json --- lib/update-linkstatus-obj.js | 45 +++++++++++++++++++++++++++++++- linkspector.js | 2 +- test/json.test.js | 50 ++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 test/json.test.js diff --git a/lib/update-linkstatus-obj.js b/lib/update-linkstatus-obj.js index 3443ccb..3c17318 100644 --- a/lib/update-linkstatus-obj.js +++ b/lib/update-linkstatus-obj.js @@ -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) @@ -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 } diff --git a/linkspector.js b/linkspector.js index bffdf1d..d39ac07 100644 --- a/linkspector.js +++ b/linkspector.js @@ -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 { diff --git a/test/json.test.js b/test/json.test.js new file mode 100644 index 0000000..379483a --- /dev/null +++ b/test/json.test.js @@ -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, + }, + ]) + }) +})