Skip to content

Commit

Permalink
feat: add feed plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Jun 5, 2021
1 parent 75a5cf9 commit bc38ce8
Show file tree
Hide file tree
Showing 22 changed files with 2,078 additions and 1 deletion.
23 changes: 23 additions & 0 deletions packages/feed/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
The MIT License (MIT)

Copyright (C) 2020 by MrHope
Copyright (c) 2019 - present webmasterish
Copyright (c) 2015 Stian Grytøyr

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.
46 changes: 46 additions & 0 deletions packages/feed/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "@mr-hope/vuepress-plugin-feed",
"version": "2.0.0-alpha.0",
"description": "Feed plugin for vuepress-theme-hope",
"keywords": [
"vuepress-plugin",
"vuepress",
"feed"
],
"homepage": "https://vuepress-theme-hope.github.io/feed/",
"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/feed"
},
"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",
"chalk": "^4.1.1",
"xml-js": "^1.6.11"
},
"devDependencies": {
"rollup": "2.50.6"
},
"publishConfig": {
"access": "public"
}
}
26 changes: 26 additions & 0 deletions packages/feed/readme.md
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-feed</h1>
<h4 align="center">VuePress feed plugin 📡 / VuePress Feed 插件 📡</h4>

[![Version](https://img.shields.io/npm/v/@mr-hope/vuepress-plugin-feed.svg?style=flat-square&logo=npm) ![Downloads](https://img.shields.io/npm/dm/@mr-hope/vuepress-plugin-feed.svg?style=flat-square&logo=npm) ![Size](https://img.shields.io/bundlephobia/min/@mr-hope/vuepress-plugin-feed?style=flat-square&logo=npm)](https://www.npmjs.com/package/@mr-hope/vuepress-plugin-feed)

<!-- markdownlint-restore -->

VuePress feed plugin 📡 / VuePress Feed 插件 📡

## [Official Docs](https://vuepress-theme-hope.github.io/feed/) | [官方文档](https://vuepress-theme-hope.github.io/feed/zh/)

## Usage / 使用

```bash
npm i -D @mr-hope/vuepress-plugin-feed
```

or

```bash
yarn add -D @mr-hope/vuepress-plugin-feed
```
5 changes: 5 additions & 0 deletions packages/feed/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { rollupTypescript } from "../../script/rollup";

export default rollupTypescript("node/index", {
external: ["@mr-hope/vuepress-shared", "chalk", "fs-extra", "path", "xml-js"],
});
153 changes: 153 additions & 0 deletions packages/feed/src/node/atom/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import * as convert from "xml-js";
import { encodeCDATA, encodeXML, generator } from "../utils";

import type { Feed } from "../feed";
import type { FeedAuthor, FeedCategory } from "../types";
import type {
AtomAuthor,
AtomCategory,
AtomContent,
AtomEntry,
} from "./typings";

const genAuthororContributor = (author: FeedAuthor): AtomAuthor => {
const { name, email, url } = author;

return {
name,
...(email ? { email } : {}),
...(url ? { uri: encodeXML(url) } : {}),
};
};

const genCategory = (category: FeedCategory): AtomCategory => {
const { name, scheme } = category;

return {
_attributes: {
term: name,
scheme,
},
};
};

/**
* Returns an Atom 1.0 feed
*
* @see http://www.atomenabled.org/developers/syndication/
*/
export const renderAtom = (feed: Feed): string => {
const { channel, links } = feed.options;

const content: AtomContent = {
_declaration: {
_attributes: {
version: "1.0",
encoding: "utf-8",
},
},
feed: {
_attributes: {
xmlns: "http://www.w3.org/2005/Atom",
...(channel.language ? { "xml:lang": channel.language } : {}),
},
id: channel.link,
title: channel.title,

...(channel.description ? { subtitle: channel.description } : {}),

...(channel.author
? { author: genAuthororContributor(channel.author) }
: {}),

updated: channel.lastUpdated
? channel.lastUpdated.toISOString()
: new Date().toISOString(),
generator: generator,
link: [{ _attributes: { rel: "self", href: encodeXML(links.atom) } }],
},
};

if (channel.link)
content.feed.link.push({
_attributes: { rel: "alternate", href: encodeXML(channel.link) },
});

if (channel.hub)
content.feed.link.push({
_attributes: { rel: "hub", href: encodeXML(channel.hub) },
});

if (channel.image) content.feed.logo = channel.image;

if (channel.icon) content.feed.icon = channel.icon;

if (channel.copyright) content.feed.rights = channel.copyright;

content.feed.category = Array.from(feed.categories).map((category) => ({
_attributes: { term: category },
}));

content.feed.contributor = Array.from(feed.contributors)
.filter((contributor) => contributor.name)
.map((contributor) => genAuthororContributor(contributor));

/**
* "entry" nodes
*/
content.feed.entry = feed.items.map((item) => {
// entry: required elements
const entry: AtomEntry = {
title: { _attributes: { type: "html" }, _text: encodeXML(item.title) },
id: encodeXML(item.guid || item.link),
link: { _attributes: { href: encodeXML(item.link) } },
updated: item.lastUpdated.toISOString(),
};

// entry: recommended elements
if (item.description)
entry.summary = {
_attributes: { type: "html" },
_cdata: encodeCDATA(item.description),
};

if (item.content)
entry.content = {
_attributes: { type: "html" },
_cdata: encodeCDATA(item.content),
};

// author(s)
if (Array.isArray(item.author))
entry.author = item.author
.filter((author) => author.name)
.map((author) => genAuthororContributor(author));
else if (item.author && item.author.name)
entry.author = [genAuthororContributor(item.author)];

if (Array.isArray(item.category))
// category
entry.category = item.category.map((category) => genCategory(category));
else if (item.category) entry.category = [genCategory(item.category)];

// contributor
if (Array.isArray(item.contributor))
entry.contributor = item.contributor.map((contributor) =>
genAuthororContributor(contributor)
);

// published
if (item.pubDate) entry.published = item.pubDate.toISOString();

// rights
if (item.copyright) entry.rights = item.copyright;

return entry;
});

return convert.js2xml(content, {
compact: true,
ignoreComment: true,
spaces: 2,
});
};
Loading

0 comments on commit bc38ce8

Please sign in to comment.