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
1 parent
b00b57e
commit 8bf93f1
Showing
12 changed files
with
613 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Source Code | ||
src/ | ||
|
||
# Test Files | ||
__tests__/ | ||
|
||
# Rollup Config files | ||
rollup.config.js | ||
|
||
# Typescript Config files | ||
tsconfig.json |
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,22 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (C) 2020 by MrHope | ||
Copyright (c) 2020 Loris Leiva | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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,45 @@ | ||
{ | ||
"name": "@mr-hope/vuepress-plugin-seo", | ||
"version": "2.0.0-alpha.0", | ||
"description": "SEO plugin for vuepress", | ||
"keywords": [ | ||
"vuepress-plugin", | ||
"seo", | ||
"search", | ||
"share" | ||
], | ||
"homepage": "https://vuepress-theme-hope.github.io/seo/", | ||
"bugs": { | ||
"url": "https://github.com/vuepress-theme-hope/vuepress-theme-hope/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/vuepress-theme-hope/vuepress-theme-hope.git", | ||
"directory": "packages/seo" | ||
}, | ||
"license": "MIT", | ||
"author": { | ||
"name": "Mr.Hope", | ||
"email": "zhangbowang1998@gmail.com", | ||
"url": "https://mrhope.site" | ||
}, | ||
"main": "node/index.js", | ||
"types": "node/index.d.ts", | ||
"files": [ | ||
"node" | ||
], | ||
"scripts": { | ||
"build": "rollup -c", | ||
"clean": "rimraf ./node", | ||
"dev": "rollup -c -w" | ||
}, | ||
"dependencies": { | ||
"@mr-hope/vuepress-shared": "2.0.0-alpha.0", | ||
"@types/fs-extra": "^9.0.11", | ||
"chalk": "^4.1.1", | ||
"fs-extra": "^10.0.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
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,26 @@ | ||
<!-- markdownlint-disable --> | ||
<p align="center"> | ||
<img width="240" src="https://vuepress-theme-hope.github.io/logo.svg" style="text-align: center;"/> | ||
</p> | ||
<h1 align="center">@mr-hope/vuepress-plugin-seo</h1> | ||
<h4 align="center">VuePress SEO plugin🛠 / VuePress SEO 插件🛠</h4> | ||
|
||
[![Version](https://img.shields.io/npm/v/@mr-hope/vuepress-plugin-seo.svg?style=flat-square&logo=npm) ![Downloads](https://img.shields.io/npm/dm/@mr-hope/vuepress-plugin-seo.svg?style=flat-square&logo=npm) ![Size](https://img.shields.io/bundlephobia/min/@mr-hope/vuepress-plugin-seo?style=flat-square&logo=npm)](https://www.npmjs.com/package/@mr-hope/vuepress-plugin-seo) | ||
|
||
<!-- markdownlint-restore --> | ||
|
||
VuePress SEO plugin🛠 / VuePress SEO 插件 🛠 | ||
|
||
## [Official Docs](https://vuepress-theme-hope.github.io/seo/) | [官方文档](https://vuepress-theme-hope.github.io/seo/zh/) | ||
|
||
## 安装 / Install | ||
|
||
```bash | ||
npm i -D @mr-hope/vuepress-plugin-seo | ||
``` | ||
|
||
Or | ||
|
||
```bash | ||
yarn add -D @mr-hope/vuepress-plugin-seo | ||
``` |
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,13 @@ | ||
import { rollupTypescript } from "../../script/rollup"; | ||
|
||
export default rollupTypescript("node/index", { | ||
external: [ | ||
"@mr-hope/vuepress-shared", | ||
"@vuepress/core", | ||
"@vuepress/client", | ||
"chalk", | ||
"fs-extra", | ||
"path", | ||
"sitemap", | ||
], | ||
}); |
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,54 @@ | ||
import { resolvePagePermalink } from "@vuepress/core"; | ||
import { generateRobotsTxt, generateSeo } from "./seo"; | ||
import { appendMeta } from "./meta"; | ||
|
||
import type { Page, Plugin } from "@vuepress/core"; | ||
import type { PageSeoInfo, SeoContent, SeoOptions } from "./types"; | ||
|
||
export * from "./types"; | ||
|
||
export const seoPlugin: Plugin<SeoOptions> = (options, app) => { | ||
const { base, themeConfig } = app.options; | ||
|
||
const seoOption = | ||
Object.keys(options).length > 0 | ||
? options | ||
: (themeConfig.seo as SeoOptions) || {}; | ||
|
||
return { | ||
name: "seo", | ||
|
||
extendsPageData(page): void { | ||
const meta = page.frontmatter.head || []; | ||
const pageSeoInfo: PageSeoInfo = { | ||
page: page as Page & { lastUpdatedTime?: number } & Record< | ||
string, | ||
unknown | ||
>, | ||
app, | ||
permalink: resolvePagePermalink(page), | ||
}; | ||
const metaContext: SeoContent = { | ||
...generateSeo(seoOption, base, pageSeoInfo), | ||
...(seoOption.seo ? seoOption.seo(pageSeoInfo) : {}), | ||
}; | ||
|
||
appendMeta(meta, metaContext, seoOption); | ||
if (seoOption.customHead) seoOption.customHead(meta, pageSeoInfo); | ||
|
||
page.frontmatter.meta = meta; | ||
}, | ||
|
||
async generated(): Promise<void> { | ||
await generateRobotsTxt(app.dir); | ||
}, | ||
|
||
plugins: [ | ||
["@mr-hope/last-update", themeConfig.lastUpdate || true], | ||
|
||
["@vuepress/last-updated", false], | ||
], | ||
}; | ||
}; | ||
|
||
export default seoPlugin; |
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,57 @@ | ||
import type { HeadConfig } from "@vuepress/core"; | ||
import type { ArticleSeoContent, SeoContent, SeoOptions } from "./types"; | ||
|
||
interface MetaOptions { | ||
name: string; | ||
content: string; | ||
attribute?: string; | ||
} | ||
|
||
const addMeta = ( | ||
meta: HeadConfig[], | ||
{ | ||
name, | ||
content, | ||
attribute = ["article:", "og:"].some((type) => name.startsWith(type)) | ||
? "property" | ||
: "name", | ||
}: MetaOptions | ||
): void => { | ||
if (content) meta.push(["meta", { [attribute]: name, content }]); | ||
}; | ||
|
||
export const appendMeta = ( | ||
head: HeadConfig[], | ||
content: SeoContent, | ||
options: SeoOptions | ||
): void => { | ||
for (const property in content) | ||
switch (property) { | ||
case "article:tag": | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
(content as ArticleSeoContent)["article:tag"]!.forEach((tag: string) => | ||
addMeta(head, { name: "article:tag", content: tag }) | ||
); | ||
break; | ||
case "og:locale:alternate": | ||
content["og:locale:alternate"].forEach((locale: string) => { | ||
if (locale !== content["og:locale"]) | ||
addMeta(head, { name: "og:locale:alternate", content: locale }); | ||
}); | ||
break; | ||
default: | ||
addMeta(head, { | ||
name: property, | ||
content: content[property as keyof SeoContent] as string, | ||
}); | ||
} | ||
|
||
if (options.restrictions) | ||
addMeta(head, { | ||
name: "og:restrictions:age", | ||
content: options.restrictions, | ||
}); | ||
|
||
if (options.twitterID) | ||
addMeta(head, { name: "twitter:creator", content: options.twitterID }); | ||
}; |
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,112 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
import { getDate } from "@mr-hope/vuepress-shared"; | ||
import { black, blue, cyan } from "chalk"; | ||
import { readFile, existsSync, writeFile } from "fs-extra"; | ||
import { getLocales, resolveUrl } from "./utils"; | ||
|
||
import type { AppDir } from "@vuepress/core"; | ||
|
||
import type { PageSeoInfo, SeoContent, SeoOptions } from "./types"; | ||
|
||
export const generateSeo = ( | ||
options: SeoOptions, | ||
base: string, | ||
{ page, app, permalink }: PageSeoInfo | ||
): SeoContent => { | ||
const { | ||
frontmatter: { | ||
author: pageAuthor, | ||
date, | ||
banner, | ||
cover, | ||
tag, | ||
tags = tag as string[], | ||
}, | ||
lastUpdatedTime, | ||
} = page; | ||
const { siteData } = app; | ||
const locales = getLocales(siteData.locales); | ||
|
||
const type = ["article", "category", "tag", "timeline"].some( | ||
(folder) => | ||
page.filePathRelative && page.filePathRelative.startsWith(`/${folder}`) | ||
) | ||
? "website" | ||
: "article"; | ||
|
||
const author = | ||
pageAuthor === false | ||
? "" | ||
: (pageAuthor as string) || | ||
options.author || | ||
(app.options.themeConfig.author as string | undefined) || | ||
""; | ||
const modifiedTime = | ||
typeof lastUpdatedTime === "number" | ||
? new Date(lastUpdatedTime).toISOString() | ||
: ""; | ||
const articleTags: string[] = Array.isArray(tags) | ||
? tags | ||
: typeof tag === "string" | ||
? [tag] | ||
: []; | ||
|
||
let publishTime = ""; | ||
|
||
if (date instanceof Date) publishTime = new Date(date).toISOString(); | ||
else if (date) { | ||
const dateInfo = getDate(date); | ||
if (dateInfo && dateInfo.value) publishTime = dateInfo.value.toISOString(); | ||
} | ||
|
||
return { | ||
"og:url": resolveUrl(base, permalink || page.path), | ||
"og:site_name": siteData.title, | ||
"og:title": page.title, | ||
"og:description": page.frontmatter.description || "", | ||
"og:type": type, | ||
"og:image": cover | ||
? resolveUrl(base, cover) | ||
: banner | ||
? resolveUrl(base, banner) | ||
: "", | ||
"og:updated_time": modifiedTime, | ||
"og:locale": page.lang, | ||
"og:locale:alternate": locales, | ||
|
||
"twitter:card": "summary_large_image", | ||
"twitter:image:alt": siteData.title, | ||
|
||
"article:author": author, | ||
"article:tag": articleTags, | ||
"article:published_time": publishTime, | ||
"article:modified_time": modifiedTime, | ||
}; | ||
}; | ||
|
||
export const generateRobotsTxt = async (dir: AppDir): Promise<void> => { | ||
console.log(blue("SEO:"), black.bgYellow("wait"), "Generating robots.txt..."); | ||
const publicPath = dir.public("robots.txt"); | ||
|
||
let content = existsSync(publicPath) | ||
? await readFile(publicPath, { encoding: "utf8" }) | ||
: ""; | ||
|
||
if (content && !content.includes("User-agent")) | ||
console.log( | ||
blue("SEO:"), | ||
black.bgRed("error"), | ||
"robots.txt seems invalid!" | ||
); | ||
else content += "\nUser-agent:*\nDisallow:\n"; | ||
|
||
await writeFile(dir.dest("robots.txt"), content, { | ||
flag: "w", | ||
}); | ||
|
||
console.log( | ||
blue("SEO:"), | ||
black.bgGreen("Success"), | ||
`${cyan("robots.txt")} generated` | ||
); | ||
}; |
Oops, something went wrong.