This repository has been archived by the owner on Feb 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
366 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* eslint-disable import/prefer-default-export */ | ||
/** | ||
* Checks the alt text of images in a markdown string. | ||
* | ||
* @param {string} markdown - The markdown string to check. | ||
* @returns {string[]} - An array of URLs of images with missing alt text. | ||
*/ | ||
export function checkAltText(markdown) { | ||
const regex = /!\[(.*?)\]\((.*?)\)/g; | ||
const matches = markdown.match(regex); | ||
const missingAltTextUrls = []; | ||
|
||
if (!matches) return missingAltTextUrls; | ||
|
||
for (const match of matches) { | ||
const [, altText, url] = match.match(/\[(.*?)\]\((.*?)\)/); | ||
if (!altText && url.startsWith('http')) { | ||
missingAltTextUrls.push(url); | ||
} | ||
} | ||
|
||
return missingAltTextUrls; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* Compares two links and checks if they have the same host and pathname. | ||
* | ||
* @param {string} link1 - The first link to compare. | ||
* @param {string} link2 - The second link to compare. | ||
* @returns {boolean} - Returns true if the links have the same host and pathname, otherwise false. | ||
*/ | ||
export function compareLink(link1, link2, site) { | ||
const url1 = new URL(link1.trim(), site); | ||
const url2 = new URL(link2.trim(), site); | ||
|
||
return (url1.host === url2.host) && (url1.pathname === url2.pathname); | ||
} | ||
/** | ||
* Extracts links from markdown content. | ||
* | ||
* @param {string} content - The markdown content. | ||
* @returns {string[]} - An array of links extracted from the content. | ||
*/ | ||
export function extractLinks(content) { | ||
const regex = /\[.*?\]\((.*?)\)/g; | ||
const links = []; | ||
let match = regex.exec(content); | ||
while (match !== null) { | ||
const link = match[1]; | ||
if (link.startsWith('http')) { | ||
links.push(link); | ||
} | ||
match = regex.exec(content); | ||
} | ||
return links; | ||
} | ||
|
||
/** | ||
* Compares two arrays of links and returns an object indicating if they match and the unique links. | ||
* | ||
* @param {Array} links1 - The first array of links. | ||
* @param {Array} links2 - The second array of links. | ||
* @returns {Promise<object>} - Match status and unique links. | ||
*/ | ||
export function compareLinks(links1, links2) { | ||
const result = { match: false, unique: [] }; | ||
|
||
result.links = links1.map((link1, index) => { | ||
const link2 = links2[index]; | ||
const match = (link1 && link2) ? compareLink(link1, link2) : false; | ||
|
||
return { link: index, link1, link2, match }; | ||
}); | ||
|
||
result.unique = result.links.filter((link) => !link.match); | ||
result.match = result.unique.length === 0; | ||
|
||
return result; | ||
} | ||
|
||
export function compareMarkdown(content1, content2, site = 'https://business.adobe.com/') { | ||
const links1 = extractLinks(content1); | ||
const links2 = extractLinks(content2); | ||
|
||
return compareLinks(links1, links2, site); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import fs from 'fs'; | ||
import { docx2md } from '@adobe/helix-docx2md'; | ||
import { entryToPath } from '../document-manager/document-manager.js'; | ||
import { compareMarkdown } from './links.js'; | ||
import { checkAltText } from './images.js'; | ||
|
||
export function checkLinks(md, markdown, reporter, entry) { | ||
const links = compareMarkdown(md, markdown); | ||
|
||
if (links) { | ||
console.log(`Links Match: ${links.match}, ${links.unique.length} unique links found.`); | ||
if (links.unique.length) { | ||
reporter?.log('validation', 'error', 'Unique links found', { entry, count: links.unique.length }); | ||
console.table(links.unique); | ||
} | ||
} else { | ||
console.log('Could not validate links'); | ||
} | ||
} | ||
|
||
export function checkImages(md, markdown, reporter, entry) { | ||
const missingAltText = checkAltText(md, markdown); | ||
console.log(`Images Missing Alt Text: ${missingAltText.length}`); | ||
if (missingAltText.length > 0) { | ||
reporter?.log('validation', 'error', 'Missing alt text', { entry, count: missingAltText.length }); | ||
console.log(missingAltText); | ||
} | ||
} | ||
|
||
export async function validateMigration(document, config) { | ||
const { markdown, entry } = document; | ||
const { reporter, outputDir } = config; | ||
const output = `${outputDir}${entryToPath(entry)}.docx`; | ||
|
||
if (!fs.existsSync(output)) return; | ||
|
||
try { | ||
const docx = await fs.promises.readFile(output); | ||
const outputMd = await docx2md(docx, { listener: null }); | ||
checkLinks(outputMd, markdown, reporter, entry); | ||
checkImages(outputMd, markdown, reporter, entry); | ||
} catch (error) { | ||
console.error('Error validating migration:', error); | ||
reporter?.log('validation', 'error', 'Error validating migration', { entry, error: error.message }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.