@nera-static/plugin-link-attributes
Version:
A plugin for static side generator Nera to add attributes to external links.
42 lines (33 loc) • 959 B
JavaScript
import path from 'path'
import { load } from 'cheerio'
import { getConfig } from '@nera-static/plugin-utils'
const HOST_CONFIG_PATH = path.resolve(
process.cwd(),
'config/link-attributes.yaml'
)
const config = getConfig(HOST_CONFIG_PATH)
const cheerioLoad = load
function addAttributesToLinks(content) {
const $ = cheerioLoad(content, null, false)
$('a[href^="http"], a[href^="www"]').each((_, el) => {
const $el = $(el)
config.attributes.forEach((attr) => {
const [name, value] = attr.split('=')
if (!$el.attr(name)) {
$el.attr(name, value?.replace(/^"|"$/g, '') ?? true)
}
})
})
return $.html()
}
export function getMetaData(data) {
if (!config) {
return data.pagesData
}
return data.pagesData.map(({ content, meta }) => {
return {
content: addAttributesToLinks(content),
meta,
}
})
}