vuepress-plugin-feed2
Advanced tools
Comparing version 2.0.0-beta.39 to 2.0.0-beta.40
@@ -387,2 +387,10 @@ import { Page, Plugin, PluginConfig } from '@vuepress/core'; | ||
/** | ||
* Custom component or element which needs to be removed | ||
* | ||
* 需要移除的自定义组件或元素 | ||
* | ||
* @default ['ExternalLinkIcon'] | ||
*/ | ||
customElements?: string[]; | ||
/** | ||
* A custom filter funciton, used to filter feed items. | ||
@@ -389,0 +397,0 @@ * |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("@mr-hope/vuepress-shared"),e=require("@vuepress/utils"),i=require("path"),a=require("xml-js");function r(t){if(t&&t.__esModule)return t;var e=Object.create(null);return t&&Object.keys(t).forEach((function(i){if("default"!==i){var a=Object.getOwnPropertyDescriptor(t,i);Object.defineProperty(e,i,a.get?a:{enumerable:!0,get:function(){return t[i]}})}})),e.default=t,Object.freeze(e)}var o=r(a);const n=new t.Logger("vuepress-plugin-feed2"),s=(t,e)=>t&&t instanceof Date?e&&e instanceof Date?e.getTime()-t.getTime():-1:1,u=t=>t.replace(/ class=".*?"/gu,"").replace(/ v-pre/gu,"").replace(/<a href="#.*?">.*?<\/a>/gu,"").replace(/(<!--.*?--!?>)|(<!--[\S\s]+?--!?>)|(<!--[\S\s]*?$)/gu,"").replace(/<OutboundLink ?\/>/gu,"").replace(/<RouterLink to="(.*?)">(.*?)<\/RouterLink>/gu,'<a href="$1">$2</a>').replace(/<(?:a|div|span)[^>]*?\/>/gu,"").replace(/<(Badge|FlowChart|Presentation).*?(?:>.*?<\/\1>|\/>)/gu,"<i>Content not supported</i>").replace(/<math[\s\S]*?\/math>/gu,"<i>Content not supported</i>"),l=(t,e="",i="")=>`${t}${e.replace(/^\/?/u,"/").replace(/\/?$/u,"/")}${i.replace(/^\//u,"")}`,p=(t="")=>`image/${"jpg"===t?"jpeg":"svg"===t?"svg+xml":"jpeg"===t||"png"===t||"bmp"===t||"gif"===t||"webp"===t?t:""}`,c=(e,i,a="")=>{var r,o,n,s,u,p,c;const{hostname:d,icon:h,image:g}=i,{base:m}=e.options,f=null===(o=null===(r=i.channel)||void 0===r?void 0:r.author)||void 0===o?void 0:o.name,y={title:(null===(n=e.siteData.locales[a])||void 0===n?void 0:n.title)||e.siteData.title||(null===(s=e.siteData.locales["/"])||void 0===s?void 0:s.title)||"",link:l(d,m,a),description:(null===(u=e.siteData.locales[a])||void 0===u?void 0:u.description)||e.siteData.description||(null===(p=e.siteData.locales["/"])||void 0===p?void 0:p.description)||"",language:(null===(c=e.siteData.locales[a])||void 0===c?void 0:c.lang)||e.siteData.lang,copyright:f?`Copyright by ${f}`:"",pubDate:new Date,lastUpdated:new Date,...h?{icon:h}:{},...g?{image:g}:{},...f?{author:{name:f}}:{}};return t.deepAssign(y,i.channel||{})},d=(t,e="/")=>({atomOutputFilename:`${e.replace(/^\//,"")}${t.atomOutputFilename||"atom.xml"}`,jsonOutputFilename:`${e.replace(/^\//,"")}${t.jsonOutputFilename||"feed.json"}`,rssOutputFilename:`${e.replace(/^\//,"")}${t.rssOutputFilename||"rss.xml"}`}),h=(t,e)=>{const{base:i}=t.options,{hostname:a}=e,{atomOutputFilename:r,jsonOutputFilename:o,rssOutputFilename:n}=d(e);return{atom:l(a,i,r),json:l(a,i,o),rss:l(a,i,n)}},g=e=>{const{name:i,email:a,url:r}=e;return{name:i,...a?{email:a}:{},...r?{uri:t.encodeXML(r)}:{}}},m=t=>{const{name:e,scheme:i}=t;return{_attributes:{term:e,scheme:i}}},f=t=>({name:t.name,...t.url?{url:t.url}:{},...t.avatar?{avatar:t.avatar}:{}}),y=t=>{const{name:e,domain:i}=t;return{_text:e,...i?{_attributes:{domain:i}}:{}}},b=e=>{const i=e.guid||t.encodeXML(e.link);return{...t.isUrl(i)?{}:{_attributes:{isPermaLink:!1}},_text:i}};class v{constructor(e){this.options=e,this.items=[],this.categories=new Set,this._contributorKeys=new Set,this.contributors=[],this.addItem=t=>{this.items.push(t)},this.addCategory=t=>{this.categories.add(t)},this.addContributor=t=>{const e=t.email||t.name;e&&!this._contributorKeys.has(e)&&(this._contributorKeys.add(e),this.contributors.push(t))},this.atom=()=>(e=>{const{channel:i,links:r}=e.options,o={_declaration:{_attributes:{version:"1.0",encoding:"utf-8"}},feed:{_attributes:{xmlns:"http://www.w3.org/2005/Atom",...i.language?{"xml:lang":i.language}:{}},id:i.link,title:i.title,...i.description?{subtitle:i.description}:{},...i.author?{author:g(i.author)}:{},updated:i.lastUpdated?i.lastUpdated.toISOString():(new Date).toISOString(),generator:"vuepress-plugin-feed2",link:[{_attributes:{rel:"self",href:t.encodeXML(r.atom)}}]}};return i.link&&o.feed.link.push({_attributes:{rel:"alternate",href:t.encodeXML(i.link)}}),i.hub&&o.feed.link.push({_attributes:{rel:"hub",href:t.encodeXML(i.hub)}}),i.image&&(o.feed.logo=i.image),i.icon&&(o.feed.icon=i.icon),i.copyright&&(o.feed.rights=i.copyright),o.feed.category=Array.from(e.categories).map((t=>({_attributes:{term:t}}))),o.feed.contributor=Array.from(e.contributors).filter((t=>t.name)).map((t=>g(t))),o.feed.entry=e.items.map((e=>{const i={title:{_attributes:{type:"html"},_text:t.encodeXML(e.title)},id:t.encodeXML(e.guid||e.link),link:{_attributes:{href:t.encodeXML(e.link)}},updated:e.lastUpdated.toISOString()};return e.description&&(i.summary=e.description.startsWith("html:")?{_attributes:{type:"html"},_cdata:t.encodeCDATA(e.description.substring(5))}:{_attributes:{type:"html"},_text:e.description}),e.content&&(i.content={_attributes:{type:"html"},_cdata:t.encodeCDATA(e.content)}),Array.isArray(e.author)?i.author=e.author.filter((t=>t.name)).map((t=>g(t))):e.author&&e.author.name&&(i.author=[g(e.author)]),Array.isArray(e.category)?i.category=e.category.map((t=>m(t))):e.category&&(i.category=[m(e.category)]),Array.isArray(e.contributor)&&(i.contributor=e.contributor.map((t=>g(t)))),e.pubDate&&(i.published=e.pubDate.toISOString()),e.copyright&&(i.rights=e.copyright),i})),a.js2xml(o,{compact:!0,ignoreComment:!0,spaces:2})})(this),this.rss=()=>(e=>{const{links:i,channel:a}=e.options;let r=!1;const n={_declaration:{_attributes:{version:"1.0",encoding:"utf-8"}},rss:{_attributes:{version:"2.0","xmlns:atom":"http://www.w3.org/2005/Atom"},channel:{"atom:link":{_attributes:{href:i.rss,rel:"self",type:"application/rss+xml"}},title:{_text:a.title},link:{_text:t.encodeXML(a.link)},description:{_text:a.description},language:{_text:t.encodeXML(a.language)},pubDate:{_text:a.pubDate?a.pubDate.toUTCString():(new Date).toUTCString()},lastBuildDate:{_text:a.lastUpdated?a.lastUpdated.toUTCString():(new Date).toUTCString()},generator:{_text:"vuepress-plugin-feed2"},docs:{_text:"https://validator.w3.org/feed/docs/rss2.html"}}}};return a.copyright&&(n.rss.channel.copyright={_text:a.copyright}),a.ttl&&(n.rss.channel.ttl={_text:a.ttl.toString()}),a.image&&(n.rss.channel.image={title:{_text:a.title},url:{_text:a.image},link:{_text:t.encodeXML(a.link)}}),n.rss.channel.category=Array.from(e.categories).map((t=>({_text:t}))),n.rss.channel.item=e.items.map((e=>{const a={title:{_text:t.encodeXML(e.title)},link:{_text:t.encodeXML(e.link)},guid:b(e),source:{_attributes:{url:i.rss},_text:e.title}};if(e.description&&(a.description={_text:e.description.startsWith("html:")?t.stripTags(e.description.substring(5)):e.description}),Array.isArray(e.author)){const t=e.author.find((t=>t.email&&t.name));t&&(a.author={_text:`${t.email} (${t.name})`})}else if("object"==typeof e.author){const{name:t,email:i}=e.author;i&&t&&(a.author={_text:`${i} (${t})`})}var o;return Array.isArray(e.category)?a.category=e.category.filter((t=>t.name)).map((t=>y(t))):"object"==typeof e.category&&e.category.name&&(a.category=[y(e.category)]),e.comments&&(a.comments={_text:t.encodeXML(e.link)}),e.pubDate&&(a.pubDate={_text:e.pubDate.toUTCString()}),e.content&&(r=!0,a["content:encoded"]={_cdata:t.encodeCDATA(e.content)}),e.enclosure&&(a.enclosure={_attributes:{url:(o=e.enclosure).url,...o.length?{length:o.length}:{},...o.type?{type:o.type}:{}}}),a})),r&&(n.rss._attributes["xmlns:content"]="http://purl.org/rss/1.0/modules/content/",n.rss._attributes["xmlns:dc"]="http://purl.org/dc/elements/1.1/"),o.js2xml(n,{compact:!0,ignoreComment:!0,spaces:2})})(this),this.json=()=>(e=>{var i;const{channel:a,links:r}=e.options,o={version:"https://jsonfeed.org/version/1.1",title:a.title,home_page_url:a.link,feed_url:r.json};return a.description&&(o.description=a.description),a.image&&(o.icon=a.image),a.icon&&(o.favicon=a.icon),(null===(i=a.author)||void 0===i?void 0:i.name)&&(o.author={name:a.author.name,...a.author.url?{url:a.author.url}:{},...a.author.avatar?{avatar:a.author.avatar}:{}}),o.items=e.items.map((e=>{const i={title:e.title,url:e.link,id:e.guid||e.link,...e.description?{summary:e.description.startsWith("html:")?t.stripTags(e.description.substring(5)):e.description}:{},content_html:e.content};return e.image&&(i.image=e.image),e.pubDate&&(i.date_published=e.pubDate.toISOString()),e.lastUpdated&&(i.date_modified=e.lastUpdated.toISOString()),Array.isArray(e.author)?i.authors=e.author.filter((t=>t.name)).map((t=>f(t))):"object"==typeof e.author&&(i.authors=[f(e.author)]),Array.isArray(e.category)?i.tags=e.category.filter((t=>t.name)).map((t=>t.name)):e.category&&(i.tags=[e.category.name]),i})),JSON.stringify(o,null,2)})(this)}}class _{constructor(t,e,i,a){this.app=t,this.options=e,this.page=i,this.feed=a,this.base=this.app.options.base,this.frontmatter=i.frontmatter,this.getter=e.getter||{},this.pageFeedOptions=this.frontmatter.feed||{}}get title(){return"function"==typeof this.getter.title?this.getter.title(this.page):this.pageFeedOptions.title||this.page.title}get link(){return"function"==typeof this.getter.link?this.getter.link(this.page):l(this.options.hostname,this.base,this.page.path)}get description(){return"function"==typeof this.getter.description?this.getter.description(this.page):this.pageFeedOptions.description?this.pageFeedOptions.description:this.frontmatter.description?this.frontmatter.description:this.page.excerpt?`html:${u(this.app.markdown.render(this.page.excerpt))}`:void 0}get author(){var e,i;return"function"==typeof this.getter.author?this.getter.author(this.page):Array.isArray(this.pageFeedOptions.author)?this.pageFeedOptions.author:"object"==typeof this.pageFeedOptions.author?[this.pageFeedOptions.author]:!1===this.frontmatter.author?[]:this.frontmatter.author?t.getAuthor(this.frontmatter.author):(null===(e=this.options.channel)||void 0===e?void 0:e.author)?t.getAuthor(null===(i=this.options.channel)||void 0===i?void 0:i.author):[]}get category(){if("function"==typeof this.getter.category)return this.getter.category(this.page);if(Array.isArray(this.pageFeedOptions.category))return this.pageFeedOptions.category;if("object"==typeof this.pageFeedOptions.category)return[this.pageFeedOptions.category];const{categories:e,category:i=e}=this.frontmatter;return t.getCategory(i).map((t=>({name:t})))}get enclosure(){return"function"==typeof this.getter.enclosure?this.getter.enclosure(this.page):this.image?{url:this.image,type:p(this.image.split(".").pop())}:void 0}get guid(){return this.pageFeedOptions.guid||this.link}get pubDate(){if("function"==typeof this.getter.publishDate)return this.getter.publishDate(this.page);const{time:t,date:e=t}=this.page.frontmatter,{createdTime:i}=this.page.data.git||{};return e&&e instanceof Date?e:i?new Date(i):void 0}get lastUpdated(){if("function"==typeof this.getter.lastUpdateDate)return this.getter.lastUpdateDate(this.page);const{updatedTime:t}=this.page.data.git||{};return t?new Date(t):new Date}get content(){return"function"==typeof this.getter.content?this.getter.content(this.page):this.pageFeedOptions.content?this.pageFeedOptions.content:u(this.page.contentRendered)}get image(){if("function"==typeof this.getter.image)return this.getter.image(this.page);const{banner:e,cover:i}=this.frontmatter;if(e){if(t.isAbsoluteUrl(e))return l(this.options.hostname,this.app.options.base,e);if(t.isUrl(e))return e}if(i){if(t.isAbsoluteUrl(i))return l(this.options.hostname,this.app.options.base,i);if(t.isUrl(i))return i}const a=/!\[.*?\]\((.*?)\)/iu.exec(this.page.content);if(a){if(t.isAbsoluteUrl(a[1]))return l(this.options.hostname,this.app.options.base,a[1]);if(t.isUrl(a[1]))return a[1]}}get contributor(){return"function"==typeof this.getter.contributor?this.getter.contributor(this.page):Array.isArray(this.pageFeedOptions.contributor)?this.pageFeedOptions.contributor:"object"==typeof this.pageFeedOptions.contributor?[this.pageFeedOptions.contributor]:this.author}get copyright(){if("function"==typeof this.getter.copyright)return this.getter.copyright(this.page);if(this.frontmatter.copyright)return this.frontmatter.copyright;const t=this.author[0];return t&&t.name?`Copyright by ${t.name}`:void 0}getFeedItem(){const{author:t,category:e,content:i,contributor:a,copyright:r,description:o,enclosure:n,guid:s,image:u,lastUpdated:l,link:p,pubDate:c,title:d}=this;return d||o?(e&&e.forEach((t=>this.feed.addCategory(t.name))),a&&a.forEach((t=>this.feed.addContributor(t))),{title:d,link:p,description:o,author:t,category:e,enclosure:n,guid:s,pubDate:c,lastUpdated:l,content:i,image:u,contributor:a,copyright:r}):null}}class O{constructor(t,e){this.app=t,this.options=e,this.feedMap=Object.fromEntries(Object.entries(e).map((([e,i])=>[e,new v({channel:c(t,i,e),links:h(t,i)})])))}addPages(t){const i=this.feedMap[t],a=this.options[t],{filter:r=(({frontmatter:t,filePathRelative:e})=>!(t.home||!e||!1===t.article||!1===t.feed)),sorter:o=((t,e)=>{var i,a,r,o;return s((null===(i=t.data.git)||void 0===i?void 0:i.createdTime)?new Date(null===(a=t.data.git)||void 0===a?void 0:a.createdTime):t.frontmatter.date,(null===(r=e.data.git)||void 0===r?void 0:r.createdTime)?new Date(null===(o=e.data.git)||void 0===o?void 0:o.createdTime):e.frontmatter.date)})}=a,u=this.app.pages.filter((e=>e.pathLocale===t)).filter(r).sort(o).slice(0,a.count||100);let l=0;for(const t of u){const e=new _(this.app,a,t,i).getFeedItem();e&&(i.addItem(e),l+=1)}n.succeed(`added ${e.chalk.cyan(`${l} page(s)`)} as feed item(s) in route ${e.chalk.cyan(t)}`)}async generateFeed(){const{dest:t}=this.app.dir;await Promise.all(Object.entries(this.options).map((async([a,r])=>{if(r.atom||r.json||r.rss){const o=this.feedMap[a],{atomOutputFilename:s,jsonOutputFilename:u,rssOutputFilename:l}=d(r,a);this.addPages(a),r.atom&&(await e.fs.ensureDir(i.dirname(t(s))),await e.fs.outputFile(t(s),o.atom()),n.succeed(`Atom feed file generated and saved to ${e.chalk.cyan(s)}`)),r.json&&(await e.fs.ensureDir(i.dirname(t(u))),await e.fs.outputFile(t(u),o.json()),n.succeed(`JSON feed file generated and saved to ${e.chalk.cyan(u)}`)),r.rss&&(await e.fs.ensureDir(i.dirname(t(l))),await e.fs.outputFile(t(l),o.rss()),n.succeed(`RSS feed file generated and saved to ${e.chalk.cyan(l)}`))}})))}}const D=(t,e)=>{if(!(t=>!!t.hostname&&(t.hostname=t.hostname.replace(/\/?$/u,""),!0))(t))return n.error("Option 'hostname' is required!"),{name:"vuepress-plugin-feed2"};if(!(t=>t.locales&&Object.entries(t.locales).some((([,{atom:t,json:e,rss:i}])=>t||e||i))||Boolean(t.atom||t.json||t.rss))(t))return n.info("No requested output, the plugin won’t start!"),{name:"vuepress-plugin-feed2"};const i=((t,e)=>Object.fromEntries(Object.keys({"/":{},...t.siteData.locales}).map((t=>{var i;return[t,{filter:({frontmatter:t,filePathRelative:e})=>!(t.home||!e||!1===t.article||!1===t.feed),sorter:(t,e)=>{var i,a,r,o;return s((null===(i=t.data.git)||void 0===i?void 0:i.createdTime)?new Date(null===(a=t.data.git)||void 0===a?void 0:a.createdTime):t.frontmatter.date,(null===(r=e.data.git)||void 0===r?void 0:r.createdTime)?new Date(null===(o=e.data.git)||void 0===o?void 0:o.createdTime):e.frontmatter.date)},...e,...null===(i=e.locales)||void 0===i?void 0:i[t],hostname:e.hostname}]}))))(e,t);return{name:"vuepress-plugin-feed2",onPrepared:t=>((t,e)=>{const{base:i}=t.options,{siteData:a}=t,r=Object.keys(e);if(1===r.length){const{atomOutputFilename:t,jsonOutputFilename:r,rssOutputFilename:o}=d(e["/"]),n=(t,r,o)=>{var n;return["link",{rel:"alternate",type:o,href:l(e["/"].hostname,i,r),title:`${a.title||(null===(n=a.locales["/"])||void 0===n?void 0:n.title)||""} ${t} Feed`}]};a.head||(a.head=[]),e.atom&&a.head.push(n("Atom",t,"application/atom+xml")),e.json&&a.head.push(n("JSON",r,"application/json")),e.rss&&a.head.push(n("RSS",o,"application/rss+xml"))}else t.pages.forEach((t=>{const{pathLocale:o}=t,n=e[o];if(r.includes(o)){const{atomOutputFilename:e,jsonOutputFilename:r,rssOutputFilename:s}=d(n,o),u=(t,e,r)=>{var s,u;return["link",{rel:"alternate",type:r,href:l(n.hostname,i,e),title:`${(null===(s=a.locales[o])||void 0===s?void 0:s.title)||a.title||(null===(u=a.locales["/"])||void 0===u?void 0:u.title)||""} ${t} Feed`}]};t.frontmatter.head||(t.frontmatter.head=[]),n.atom&&t.frontmatter.head.push(u("Atom",e,"application/atom+xml")),n.json&&t.frontmatter.head.push(u("JSON",r,"application/json")),n.rss&&t.frontmatter.head.push(u("RSS",s,"application/rss+xml"))}}))})(t,i),onGenerated:async t=>{await new O(t,i).generateFeed()}}};exports.default=D,exports.feed=t=>["feed2",t],exports.feedPlugin=D; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("@mr-hope/vuepress-shared"),e=require("@vuepress/shared"),i=require("@vuepress/utils"),a=require("path"),r=require("xml-js");function o(t){if(t&&t.__esModule)return t;var e=Object.create(null);return t&&Object.keys(t).forEach((function(i){if("default"!==i){var a=Object.getOwnPropertyDescriptor(t,i);Object.defineProperty(e,i,a.get?a:{enumerable:!0,get:function(){return t[i]}})}})),e.default=t,Object.freeze(e)}var n=o(r);const s=new t.Logger("vuepress-plugin-feed2"),l=(t,e)=>t&&t instanceof Date?e&&e instanceof Date?e.getTime()-t.getTime():-1:1,u=(t,e=[])=>t.replace(/ class=".*?"/gu,"").replace(/ v-pre/gu,"").replace(/<a href="#.*?">.*?<\/a>/gu,"").replace(/(<!--.*?--!?>)|(<!--[\S\s]+?--!?>)|(<!--[\S\s]*?$)/gu,"").replace(/<ExternalLinkIcon ?\/>/gu,"").replace(/<RouterLink to="(.*?)">(.*?)<\/RouterLink>/gu,'<a href="$1">$2</a>').replace(/<(?:a|div|span)[^>]*?\/>/gu,"").replace(new RegExp(`<(${e.join("|")})[^>]*?(?:>*?<\\/\\1>|\\/>)`,"gu"),"<i>Content not supported</i>").replace(/<math[\s\S]*?\/math>/gu,"<i>Content not supported</i>"),p=(t,i="",a="")=>`${t}${i}${e.removeLeadingSlash(a)}`,c=(t="")=>`image/${"jpg"===t?"jpeg":"svg"===t?"svg+xml":"jpeg"===t||"png"===t||"bmp"===t||"gif"===t||"webp"===t?t:""}`,d=(e,i,a="")=>{var r,o,n,s,l,u,c;const{hostname:d,icon:h,image:g}=i,{base:m}=e.options,f=null===(o=null===(r=i.channel)||void 0===r?void 0:r.author)||void 0===o?void 0:o.name,y={title:(null===(n=e.siteData.locales[a])||void 0===n?void 0:n.title)||e.siteData.title||(null===(s=e.siteData.locales["/"])||void 0===s?void 0:s.title)||"",link:p(d,m,a),description:(null===(l=e.siteData.locales[a])||void 0===l?void 0:l.description)||e.siteData.description||(null===(u=e.siteData.locales["/"])||void 0===u?void 0:u.description)||"",language:(null===(c=e.siteData.locales[a])||void 0===c?void 0:c.lang)||e.siteData.lang,copyright:f?`Copyright by ${f}`:"",pubDate:new Date,lastUpdated:new Date,...h?{icon:h}:{},...g?{image:g}:{},...f?{author:{name:f}}:{}};return t.deepAssign(y,i.channel||{})},h=(t,i="/")=>({atomOutputFilename:`${e.removeLeadingSlash(i)}${t.atomOutputFilename||"atom.xml"}`,jsonOutputFilename:`${e.removeLeadingSlash(i)}${t.jsonOutputFilename||"feed.json"}`,rssOutputFilename:`${e.removeLeadingSlash(i)}${t.rssOutputFilename||"rss.xml"}`}),g=(t,e)=>{const{base:i}=t.options,{hostname:a}=e,{atomOutputFilename:r,jsonOutputFilename:o,rssOutputFilename:n}=h(e);return{atom:p(a,i,r),json:p(a,i,o),rss:p(a,i,n)}},m=e=>{const{name:i,email:a,url:r}=e;return{name:i,...a?{email:a}:{},...r?{uri:t.encodeXML(r)}:{}}},f=t=>{const{name:e,scheme:i}=t;return{_attributes:{term:e,scheme:i}}},y=t=>({name:t.name,...t.url?{url:t.url}:{},...t.avatar?{avatar:t.avatar}:{}}),b=t=>{const{name:e,domain:i}=t;return{_text:e,...i?{_attributes:{domain:i}}:{}}},v=e=>{const i=e.guid||t.encodeXML(e.link);return{...t.isUrl(i)?{}:{_attributes:{isPermaLink:!1}},_text:i}};class _{constructor(e){this.options=e,this.items=[],this.categories=new Set,this._contributorKeys=new Set,this.contributors=[],this.addItem=t=>{this.items.push(t)},this.addCategory=t=>{this.categories.add(t)},this.addContributor=t=>{const e=t.email||t.name;e&&!this._contributorKeys.has(e)&&(this._contributorKeys.add(e),this.contributors.push(t))},this.atom=()=>(e=>{const{channel:i,links:a}=e.options,o={_declaration:{_attributes:{version:"1.0",encoding:"utf-8"}},feed:{_attributes:{xmlns:"http://www.w3.org/2005/Atom",...i.language?{"xml:lang":i.language}:{}},id:i.link,title:i.title,...i.description?{subtitle:i.description}:{},...i.author?{author:m(i.author)}:{},updated:i.lastUpdated?i.lastUpdated.toISOString():(new Date).toISOString(),generator:"vuepress-plugin-feed2",link:[{_attributes:{rel:"self",href:t.encodeXML(a.atom)}}]}};return i.link&&o.feed.link.push({_attributes:{rel:"alternate",href:t.encodeXML(i.link)}}),i.hub&&o.feed.link.push({_attributes:{rel:"hub",href:t.encodeXML(i.hub)}}),i.image&&(o.feed.logo=i.image),i.icon&&(o.feed.icon=i.icon),i.copyright&&(o.feed.rights=i.copyright),o.feed.category=Array.from(e.categories).map((t=>({_attributes:{term:t}}))),o.feed.contributor=Array.from(e.contributors).filter((t=>t.name)).map((t=>m(t))),o.feed.entry=e.items.map((e=>{const i={title:{_attributes:{type:"html"},_text:t.encodeXML(e.title)},id:t.encodeXML(e.guid||e.link),link:{_attributes:{href:t.encodeXML(e.link)}},updated:e.lastUpdated.toISOString()};return e.description&&(i.summary=e.description.startsWith("html:")?{_attributes:{type:"html"},_cdata:t.encodeCDATA(e.description.substring(5))}:{_attributes:{type:"html"},_text:e.description}),e.content&&(i.content={_attributes:{type:"html"},_cdata:t.encodeCDATA(e.content)}),Array.isArray(e.author)?i.author=e.author.filter((t=>t.name)).map((t=>m(t))):e.author&&e.author.name&&(i.author=[m(e.author)]),Array.isArray(e.category)?i.category=e.category.map((t=>f(t))):e.category&&(i.category=[f(e.category)]),Array.isArray(e.contributor)&&(i.contributor=e.contributor.map((t=>m(t)))),e.pubDate&&(i.published=e.pubDate.toISOString()),e.copyright&&(i.rights=e.copyright),i})),r.js2xml(o,{compact:!0,ignoreComment:!0,spaces:2})})(this),this.rss=()=>(e=>{const{links:i,channel:a}=e.options;let r=!1;const o={_declaration:{_attributes:{version:"1.0",encoding:"utf-8"}},rss:{_attributes:{version:"2.0","xmlns:atom":"http://www.w3.org/2005/Atom"},channel:{"atom:link":{_attributes:{href:i.rss,rel:"self",type:"application/rss+xml"}},title:{_text:a.title},link:{_text:t.encodeXML(a.link)},description:{_text:a.description},language:{_text:t.encodeXML(a.language)},pubDate:{_text:a.pubDate?a.pubDate.toUTCString():(new Date).toUTCString()},lastBuildDate:{_text:a.lastUpdated?a.lastUpdated.toUTCString():(new Date).toUTCString()},generator:{_text:"vuepress-plugin-feed2"},docs:{_text:"https://validator.w3.org/feed/docs/rss2.html"}}}};return a.copyright&&(o.rss.channel.copyright={_text:a.copyright}),a.ttl&&(o.rss.channel.ttl={_text:a.ttl.toString()}),a.image&&(o.rss.channel.image={title:{_text:a.title},url:{_text:a.image},link:{_text:t.encodeXML(a.link)}}),o.rss.channel.category=Array.from(e.categories).map((t=>({_text:t}))),o.rss.channel.item=e.items.map((e=>{const a={title:{_text:t.encodeXML(e.title)},link:{_text:t.encodeXML(e.link)},guid:v(e),source:{_attributes:{url:i.rss},_text:e.title}};if(e.description&&(a.description={_text:e.description.startsWith("html:")?t.stripTags(e.description.substring(5)):e.description}),Array.isArray(e.author)){const t=e.author.find((t=>t.email&&t.name));t&&(a.author={_text:`${t.email} (${t.name})`})}else if("object"==typeof e.author){const{name:t,email:i}=e.author;i&&t&&(a.author={_text:`${i} (${t})`})}var o;return Array.isArray(e.category)?a.category=e.category.filter((t=>t.name)).map((t=>b(t))):"object"==typeof e.category&&e.category.name&&(a.category=[b(e.category)]),e.comments&&(a.comments={_text:t.encodeXML(e.link)}),e.pubDate&&(a.pubDate={_text:e.pubDate.toUTCString()}),e.content&&(r=!0,a["content:encoded"]={_cdata:t.encodeCDATA(e.content)}),e.enclosure&&(a.enclosure={_attributes:{url:(o=e.enclosure).url,...o.length?{length:o.length}:{},...o.type?{type:o.type}:{}}}),a})),r&&(o.rss._attributes["xmlns:content"]="http://purl.org/rss/1.0/modules/content/",o.rss._attributes["xmlns:dc"]="http://purl.org/dc/elements/1.1/"),n.js2xml(o,{compact:!0,ignoreComment:!0,spaces:2})})(this),this.json=()=>(e=>{var i;const{channel:a,links:r}=e.options,o={version:"https://jsonfeed.org/version/1.1",title:a.title,home_page_url:a.link,feed_url:r.json};return a.description&&(o.description=a.description),a.image&&(o.icon=a.image),a.icon&&(o.favicon=a.icon),(null===(i=a.author)||void 0===i?void 0:i.name)&&(o.author={name:a.author.name,...a.author.url?{url:a.author.url}:{},...a.author.avatar?{avatar:a.author.avatar}:{}}),o.items=e.items.map((e=>{const i={title:e.title,url:e.link,id:e.guid||e.link,...e.description?{summary:e.description.startsWith("html:")?t.stripTags(e.description.substring(5)):e.description}:{},content_html:e.content};return e.image&&(i.image=e.image),e.pubDate&&(i.date_published=e.pubDate.toISOString()),e.lastUpdated&&(i.date_modified=e.lastUpdated.toISOString()),Array.isArray(e.author)?i.authors=e.author.filter((t=>t.name)).map((t=>y(t))):"object"==typeof e.author&&(i.authors=[y(e.author)]),Array.isArray(e.category)?i.tags=e.category.filter((t=>t.name)).map((t=>t.name)):e.category&&(i.tags=[e.category.name]),i})),JSON.stringify(o,null,2)})(this)}}class O{constructor(t,e,i,a){this.app=t,this.options=e,this.page=i,this.feed=a,this.base=this.app.options.base,this.frontmatter=i.frontmatter,this.getter=e.getter||{},this.pageFeedOptions=this.frontmatter.feed||{}}get title(){return"function"==typeof this.getter.title?this.getter.title(this.page):this.pageFeedOptions.title||this.page.title}get link(){return"function"==typeof this.getter.link?this.getter.link(this.page):p(this.options.hostname,this.base,this.page.path)}get description(){return"function"==typeof this.getter.description?this.getter.description(this.page):this.pageFeedOptions.description?this.pageFeedOptions.description:this.frontmatter.description?this.frontmatter.description:this.page.excerpt?`html:${u(this.app.markdown.render(this.page.excerpt),this.options.customElements)}`:void 0}get author(){var e,i;return"function"==typeof this.getter.author?this.getter.author(this.page):Array.isArray(this.pageFeedOptions.author)?this.pageFeedOptions.author:"object"==typeof this.pageFeedOptions.author?[this.pageFeedOptions.author]:!1===this.frontmatter.author?[]:this.frontmatter.author?t.getAuthor(this.frontmatter.author):(null===(e=this.options.channel)||void 0===e?void 0:e.author)?t.getAuthor(null===(i=this.options.channel)||void 0===i?void 0:i.author):[]}get category(){if("function"==typeof this.getter.category)return this.getter.category(this.page);if(Array.isArray(this.pageFeedOptions.category))return this.pageFeedOptions.category;if("object"==typeof this.pageFeedOptions.category)return[this.pageFeedOptions.category];const{categories:e,category:i=e}=this.frontmatter;return t.getCategory(i).map((t=>({name:t})))}get enclosure(){return"function"==typeof this.getter.enclosure?this.getter.enclosure(this.page):this.image?{url:this.image,type:c(this.image.split(".").pop())}:void 0}get guid(){return this.pageFeedOptions.guid||this.link}get pubDate(){if("function"==typeof this.getter.publishDate)return this.getter.publishDate(this.page);const{time:t,date:e=t}=this.page.frontmatter,{createdTime:i}=this.page.data.git||{};return e&&e instanceof Date?e:i?new Date(i):void 0}get lastUpdated(){if("function"==typeof this.getter.lastUpdateDate)return this.getter.lastUpdateDate(this.page);const{updatedTime:t}=this.page.data.git||{};return t?new Date(t):new Date}get content(){return"function"==typeof this.getter.content?this.getter.content(this.page):this.pageFeedOptions.content?this.pageFeedOptions.content:u(this.page.contentRendered,this.options.customElements)}get image(){if("function"==typeof this.getter.image)return this.getter.image(this.page);const{banner:e,cover:i}=this.frontmatter;if(e){if(t.isAbsoluteUrl(e))return p(this.options.hostname,this.app.options.base,e);if(t.isUrl(e))return e}if(i){if(t.isAbsoluteUrl(i))return p(this.options.hostname,this.app.options.base,i);if(t.isUrl(i))return i}const a=/!\[.*?\]\((.*?)\)/iu.exec(this.page.content);if(a){if(t.isAbsoluteUrl(a[1]))return p(this.options.hostname,this.app.options.base,a[1]);if(t.isUrl(a[1]))return a[1]}}get contributor(){return"function"==typeof this.getter.contributor?this.getter.contributor(this.page):Array.isArray(this.pageFeedOptions.contributor)?this.pageFeedOptions.contributor:"object"==typeof this.pageFeedOptions.contributor?[this.pageFeedOptions.contributor]:this.author}get copyright(){if("function"==typeof this.getter.copyright)return this.getter.copyright(this.page);if(this.frontmatter.copyright)return this.frontmatter.copyright;const t=this.author[0];return t&&t.name?`Copyright by ${t.name}`:void 0}getFeedItem(){const{author:t,category:e,content:i,contributor:a,copyright:r,description:o,enclosure:n,guid:s,image:l,lastUpdated:u,link:p,pubDate:c,title:d}=this;return d||o?(e&&e.forEach((t=>this.feed.addCategory(t.name))),a&&a.forEach((t=>this.feed.addContributor(t))),{title:d,link:p,description:o,author:t,category:e,enclosure:n,guid:s,pubDate:c,lastUpdated:u,content:i,image:l,contributor:a,copyright:r}):null}}class x{constructor(t,e){this.app=t,this.options=e,this.feedMap=Object.fromEntries(Object.entries(e).map((([e,i])=>[e,new _({channel:d(t,i,e),links:g(t,i)})])))}addPages(t){const e=this.feedMap[t],a=this.options[t],{filter:r=(({frontmatter:t,filePathRelative:e})=>!(t.home||!e||!1===t.article||!1===t.feed)),sorter:o=((t,e)=>{var i,a,r,o;return l((null===(i=t.data.git)||void 0===i?void 0:i.createdTime)?new Date(null===(a=t.data.git)||void 0===a?void 0:a.createdTime):t.frontmatter.date,(null===(r=e.data.git)||void 0===r?void 0:r.createdTime)?new Date(null===(o=e.data.git)||void 0===o?void 0:o.createdTime):e.frontmatter.date)})}=a,n=this.app.pages.filter((e=>e.pathLocale===t)).filter(r).sort(o).slice(0,a.count||100);let u=0;for(const t of n){const i=new O(this.app,a,t,e).getFeedItem();i&&(e.addItem(i),u+=1)}s.succeed(`added ${i.chalk.cyan(`${u} page(s)`)} as feed item(s) in route ${i.chalk.cyan(t)}`)}async generateFeed(){const{dest:t}=this.app.dir;await Promise.all(Object.entries(this.options).map((async([e,r])=>{if(r.atom||r.json||r.rss){const o=this.feedMap[e],{atomOutputFilename:n,jsonOutputFilename:l,rssOutputFilename:u}=h(r,e);this.addPages(e),r.atom&&(await i.fs.ensureDir(a.dirname(t(n))),await i.fs.outputFile(t(n),o.atom()),s.succeed(`Atom feed file generated and saved to ${i.chalk.cyan(n)}`)),r.json&&(await i.fs.ensureDir(a.dirname(t(l))),await i.fs.outputFile(t(l),o.json()),s.succeed(`JSON feed file generated and saved to ${i.chalk.cyan(l)}`)),r.rss&&(await i.fs.ensureDir(a.dirname(t(u))),await i.fs.outputFile(t(u),o.rss()),s.succeed(`RSS feed file generated and saved to ${i.chalk.cyan(u)}`))}})))}}const D=(t,i)=>{if(!(t=>!!t.hostname&&(t.hostname=e.removeEndingSlash(t.hostname),!0))(t))return s.error("Option 'hostname' is required!"),{name:"vuepress-plugin-feed2"};if(!(t=>t.locales&&Object.entries(t.locales).some((([,{atom:t,json:e,rss:i}])=>t||e||i))||Boolean(t.atom||t.json||t.rss))(t))return s.info("No requested output, the plugin won’t start!"),{name:"vuepress-plugin-feed2"};const a=((t,e)=>Object.fromEntries(Object.keys({"/":{},...t.siteData.locales}).map((t=>{var i;return[t,{filter:({frontmatter:t,filePathRelative:e})=>!(t.home||!e||!1===t.article||!1===t.feed),sorter:(t,e)=>{var i,a,r,o;return l((null===(i=t.data.git)||void 0===i?void 0:i.createdTime)?new Date(null===(a=t.data.git)||void 0===a?void 0:a.createdTime):t.frontmatter.date,(null===(r=e.data.git)||void 0===r?void 0:r.createdTime)?new Date(null===(o=e.data.git)||void 0===o?void 0:o.createdTime):e.frontmatter.date)},...e,...null===(i=e.locales)||void 0===i?void 0:i[t],hostname:e.hostname}]}))))(i,t);return{name:"vuepress-plugin-feed2",onPrepared:t=>((t,e)=>{const{base:i}=t.options,{siteData:a}=t,r=Object.keys(e);if(1===r.length){const{atomOutputFilename:t,jsonOutputFilename:r,rssOutputFilename:o}=h(e["/"]),n=(t,r,o)=>{var n;return["link",{rel:"alternate",type:o,href:p(e["/"].hostname,i,r),title:`${a.title||(null===(n=a.locales["/"])||void 0===n?void 0:n.title)||""} ${t} Feed`}]};a.head||(a.head=[]),e.atom&&a.head.push(n("Atom",t,"application/atom+xml")),e.json&&a.head.push(n("JSON",r,"application/json")),e.rss&&a.head.push(n("RSS",o,"application/rss+xml"))}else t.pages.forEach((t=>{const{pathLocale:o}=t,n=e[o];if(r.includes(o)){const{atomOutputFilename:e,jsonOutputFilename:r,rssOutputFilename:s}=h(n,o),l=(t,e,r)=>{var s,l;return["link",{rel:"alternate",type:r,href:p(n.hostname,i,e),title:`${(null===(s=a.locales[o])||void 0===s?void 0:s.title)||a.title||(null===(l=a.locales["/"])||void 0===l?void 0:l.title)||""} ${t} Feed`}]};t.frontmatter.head||(t.frontmatter.head=[]),n.atom&&t.frontmatter.head.push(l("Atom",e,"application/atom+xml")),n.json&&t.frontmatter.head.push(l("JSON",r,"application/json")),n.rss&&t.frontmatter.head.push(l("RSS",s,"application/rss+xml"))}}))})(t,a),onGenerated:async t=>{await new x(t,a).generateFeed()}}};exports.default=D,exports.feed=t=>["feed2",t],exports.feedPlugin=D; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "vuepress-plugin-feed2", | ||
"version": "2.0.0-beta.39", | ||
"version": "2.0.0-beta.40", | ||
"description": "Feed plugin for vuepress-theme-hope", | ||
@@ -17,3 +17,3 @@ "keywords": [ | ||
"url": "git+https://github.com/vuepress-theme-hope/vuepress-theme-hope.git", | ||
"directory": "packages/feed" | ||
"directory": "packages/feed2" | ||
}, | ||
@@ -39,3 +39,4 @@ "license": "MIT", | ||
"dependencies": { | ||
"@mr-hope/vuepress-shared": "2.0.0-beta.39", | ||
"@mr-hope/vuepress-shared": "2.0.0-beta.40", | ||
"@vuepress/shared": "2.0.0-beta.38", | ||
"@vuepress/utils": "2.0.0-beta.38", | ||
@@ -50,3 +51,3 @@ "xml-js": "^1.6.11" | ||
}, | ||
"gitHead": "ce2e557a72d63b36574d212ebc52c0ae480039b6" | ||
"gitHead": "bf1434a12c49db09143e51fae9760c70c832f3f4" | ||
} |
import { deepAssign } from "@mr-hope/vuepress-shared"; | ||
import { removeEndingSlash, removeLeadingSlash } from "@vuepress/shared"; | ||
import { compareDate, resolveUrl } from "./utils"; | ||
@@ -20,3 +21,3 @@ | ||
if (options.hostname) { | ||
options.hostname = options.hostname.replace(/\/?$/u, ""); | ||
options.hostname = removeEndingSlash(options.hostname); | ||
@@ -121,9 +122,9 @@ return true; | ||
} => ({ | ||
atomOutputFilename: `${prefix.replace(/^\//, "")}${ | ||
atomOutputFilename: `${removeLeadingSlash(prefix)}${ | ||
options.atomOutputFilename || "atom.xml" | ||
}`, | ||
jsonOutputFilename: `${prefix.replace(/^\//, "")}${ | ||
jsonOutputFilename: `${removeLeadingSlash(prefix)}${ | ||
options.jsonOutputFilename || "feed.json" | ||
}`, | ||
rssOutputFilename: `${prefix.replace(/^\//, "")}${ | ||
rssOutputFilename: `${removeLeadingSlash(prefix)}${ | ||
options.rssOutputFilename || "rss.xml" | ||
@@ -130,0 +131,0 @@ }`, |
@@ -68,3 +68,6 @@ import { | ||
if (this.page.excerpt) | ||
return `html:${resolveHTML(this.app.markdown.render(this.page.excerpt))}`; | ||
return `html:${resolveHTML( | ||
this.app.markdown.render(this.page.excerpt), | ||
this.options.customElements | ||
)}`; | ||
@@ -155,3 +158,3 @@ return undefined; | ||
return resolveHTML(this.page.contentRendered); | ||
return resolveHTML(this.page.contentRendered, this.options.customElements); | ||
} | ||
@@ -158,0 +161,0 @@ |
import { Logger } from "@mr-hope/vuepress-shared"; | ||
import { removeLeadingSlash } from "@vuepress/shared"; | ||
@@ -15,3 +16,6 @@ export const logger = new Logger("vuepress-plugin-feed2"); | ||
export const resolveHTML = (html: string): string => | ||
export const resolveHTML = ( | ||
html: string, | ||
customElements: string[] = [] | ||
): string => | ||
html | ||
@@ -26,4 +30,4 @@ // remove html class | ||
.replace(/(<!--.*?--!?>)|(<!--[\S\s]+?--!?>)|(<!--[\S\s]*?$)/gu, "") | ||
// remove OutboundLink | ||
.replace(/<OutboundLink ?\/>/gu, "") | ||
// remove ExternalLinkIcon | ||
.replace(/<ExternalLinkIcon ?\/>/gu, "") | ||
// resolve RouterLink | ||
@@ -38,3 +42,6 @@ .replace( | ||
.replace( | ||
/<(Badge|FlowChart|Presentation).*?(?:>.*?<\/\1>|\/>)/gu, | ||
new RegExp( | ||
`<(${customElements.join("|")})[^>]*?(?:>*?<\\/\\1>|\\/>)`, | ||
"gu" | ||
), | ||
"<i>Content not supported</i>" | ||
@@ -46,9 +53,3 @@ ) | ||
export const resolveUrl = (hostname: string, base = "", path = ""): string => | ||
`${hostname}${ | ||
// make sure base starts and ends with '/' | ||
base.replace(/^\/?/u, "/").replace(/\/?$/u, "/") | ||
}${ | ||
// make sure path does not start with '/' | ||
path.replace(/^\//u, "") | ||
}`; | ||
`${hostname}${base}${removeLeadingSlash(path)}`; | ||
@@ -55,0 +56,0 @@ export const getImageMineType = (ext = ""): string => |
@@ -56,2 +56,11 @@ import type { Page } from "@vuepress/core"; | ||
/** | ||
* Custom component or element which needs to be removed | ||
* | ||
* 需要移除的自定义组件或元素 | ||
* | ||
* @default ['ExternalLinkIcon'] | ||
*/ | ||
customElements?: string[]; | ||
/** | ||
* A custom filter funciton, used to filter feed items. | ||
@@ -58,0 +67,0 @@ * |
Sorry, the diff of this file is not supported yet
114828
2716
4
+ Added@mr-hope/vuepress-shared@2.0.0-beta.40(transitive)
- Removed@mr-hope/vuepress-shared@2.0.0-beta.39(transitive)