fuc-new/doc/node_modules/@vuepress/plugin-seo/lib/node/appendHead.js
hellcat 6adbc04d4b 1
2025-10-03 17:17:08 +08:00

56 lines
1.8 KiB
JavaScript

import { keys, startsWith } from '@vuepress/helper';
const appendMetaToHead = (head, { name, content, attribute = ['article:', 'og:'].some((type) => startsWith(name, type))
? 'property'
: 'name', }) => {
if (content)
head.unshift(['meta', { [attribute]: name, content }]);
};
export const addOGP = (head, content) => {
keys(content)
.reverse()
.forEach((property) => {
switch (property) {
case 'article:tag':
;
content['article:tag'].forEach((tag) => {
appendMetaToHead(head, { name: 'article:tag', content: tag });
});
break;
case 'og:locale:alternate':
content['og:locale:alternate'].forEach((locale) => {
if (locale !== content['og:locale'])
appendMetaToHead(head, {
name: 'og:locale:alternate',
content: locale,
});
});
break;
default:
if (content[property])
appendMetaToHead(head, {
name: property,
content: content[property],
});
}
});
};
export const appendJSONLD = (head, content) => {
head.unshift([
'script',
{ type: 'application/ld+json' },
JSON.stringify(content),
]);
};
export const appendCanonical = (head, url) => {
if (url)
head.unshift(['link', { rel: 'canonical', href: url }]);
};
export const appendAlternate = (head, urls) => {
urls.forEach(({ lang, path }) => {
head.unshift([
'link',
{ rel: 'alternate', hreflang: lang.toLowerCase(), href: path },
]);
});
};