test02/node_modules/@vuepress/plugin-seo/lib/node/appendHead.js
罗佳鸿 6aa1ebe342
Some checks are pending
部署文档 / deploy-gh-pages (push) Waiting to run
first commit
2024-08-13 10:11:19 +08:00

51 lines
1.7 KiB
JavaScript

import { startsWith } from '@vuepress/helper';
const appendMetaToHead = (head, { name, content, attribute = ['article:', 'og:'].some((type) => startsWith(name, type))
? 'property'
: 'name', }) => {
if (content)
head.push(['meta', { [attribute]: name, content }]);
};
export const addOGP = (head, content) => {
for (const property in content)
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.push([
'script',
{ type: 'application/ld+json' },
JSON.stringify(content),
]);
};
export const appendCanonical = (head, url) => {
if (url)
head.push(['link', { rel: 'canonical', href: url }]);
};
export const appendAlternate = (head, urls) => {
urls.forEach(({ lang, path }) => {
head.push([
'link',
{ rel: 'alternate', hreflang: lang.toLowerCase(), href: path },
]);
});
};