mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-26 20:34:02 +03:00
104f84f252
As discussed with the product team we want to enforce kebab-case file names for all files, with the exception of files which export a single class, in which case they should be PascalCase and reflect the class which they export. This will help find classes faster, and should push better naming for them too. Some files and packages have been excluded from this linting, specifically when a library or framework depends on the naming of a file for the functionality e.g. Ember, knex-migrator, adapter-manager
47 lines
1.5 KiB
JavaScript
47 lines
1.5 KiB
JavaScript
class LinkReplacer {
|
|
/**
|
|
* Replaces the links in the provided HTML
|
|
* @param {string} html
|
|
* @param {(url: URL): Promise<URL|string>} replaceLink
|
|
* @returns {Promise<string>}
|
|
*/
|
|
async replace(html, replaceLink) {
|
|
const cheerio = require('cheerio');
|
|
const entities = require('entities');
|
|
try {
|
|
const $ = cheerio.load(html, {
|
|
xml: {
|
|
// This makes sure we use the faster and less destructive htmlparser2 parser
|
|
xmlMode: false
|
|
},
|
|
// Do not replace &, ', " and others with HTML entities (is bugged because it replaces &map_ with something weird (↦))
|
|
decodeEntities: false
|
|
}, false);
|
|
|
|
for (const el of $('a').toArray()) {
|
|
const href = $(el).attr('href');
|
|
if (href) {
|
|
let url;
|
|
try {
|
|
url = new URL(entities.decode(href));
|
|
} catch (e) {
|
|
// Ignore invalid URLs
|
|
}
|
|
if (url) {
|
|
url = await replaceLink(url);
|
|
const str = url.toString();
|
|
$(el).attr('href', str);
|
|
}
|
|
}
|
|
}
|
|
|
|
return $.html();
|
|
} catch (e) {
|
|
// Catch errors from cheerio
|
|
return html;
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = new LinkReplacer();
|