vuepress-plugin-feed2
Advanced tools
Comparing version 2.0.0-beta.8 to 2.0.0-beta.9
@@ -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"),r=require("xml-js");function a(t){if(t&&t.__esModule)return t;var e=Object.create(null);return t&&Object.keys(t).forEach((function(i){if("default"!==i){var r=Object.getOwnPropertyDescriptor(t,i);Object.defineProperty(e,i,r.get?r:{enumerable:!0,get:function(){return t[i]}})}})),e.default=t,Object.freeze(e)}var o=a(r);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,r="")=>{var a,o,n,s,u,p,c;const{hostname:h,icon:d,image:g}=i,{base:m}=e.options,f=null===(o=null===(a=i.channel)||void 0===a?void 0:a.author)||void 0===o?void 0:o.name,y={title:(null===(n=e.siteData.locales[r])||void 0===n?void 0:n.title)||e.siteData.title||(null===(s=e.siteData.locales["/"])||void 0===s?void 0:s.title)||"",link:l(h,m,r),description:(null===(u=e.siteData.locales[r])||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[r])||void 0===c?void 0:c.lang)||e.siteData.lang,copyright:f?`Copyright by ${f}`:"",pubDate:new Date,lastUpdated:new Date,...d?{icon:d}:{},...g?{image:g}:{},...f?{author:{name:f}}:{}};return t.deepAssign(y,i.channel||{})},h=(t,e="/")=>({atomOutputFilename:`${e.replace(/^\//,"")}${t.atomOutputFilename||"atom.xml"}`,jsonOutputFilename:`${e.replace(/^\//,"")}${t.jsonOutputFilename||"feed.json"}`,rssOutputFilename:`${e.replace(/^\//,"")}${t.rssOutputFilename||"rss.xml"}`}),d=(t,e)=>{const{base:i}=t.options,{hostname:r}=e,{atomOutputFilename:a,jsonOutputFilename:o,rssOutputFilename:n}=h(e);return{atom:l(r,i,a),json:l(r,i,o),rss:l(r,i,n)}},g=e=>{const{name:i,email:r,url:a}=e;return{name:i,...r?{email:r}:{},...a?{uri:t.encodeXML(a)}:{}}},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: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:g(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=>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})),r.js2xml(o,{compact:!0,ignoreComment:!0,spaces:2})})(this),this.rss=()=>(e=>{const{links:i,channel:r}=e.options;let a=!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:r.title},link:{_text:t.encodeXML(r.link)},description:{_text:r.description},language:{_text:t.encodeXML(r.language)},pubDate:{_text:r.pubDate?r.pubDate.toUTCString():(new Date).toUTCString()},lastBuildDate:{_text:r.lastUpdated?r.lastUpdated.toUTCString():(new Date).toUTCString()},generator:{_text:"vuepress-plugin-feed2"},docs:{_text:"https://validator.w3.org/feed/docs/rss2.html"}}}};return r.copyright&&(n.rss.channel.copyright={_text:r.copyright}),r.ttl&&(n.rss.channel.ttl={_text:r.ttl.toString()}),r.image&&(n.rss.channel.image={title:{_text:r.title},url:{_text:r.image},link:{_text:t.encodeXML(r.link)}}),n.rss.channel.category=Array.from(e.categories).map((t=>({_text:t}))),n.rss.channel.item=e.items.map((e=>{const r={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&&(r.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&&(r.author={_text:`${t.email} (${t.name})`})}else if("object"==typeof e.author){const{name:t,email:i}=e.author;i&&t&&(r.author={_text:`${i} (${t})`})}var o;return Array.isArray(e.category)?r.category=e.category.filter((t=>t.name)).map((t=>y(t))):"object"==typeof e.category&&e.category.name&&(r.category=[y(e.category)]),e.comments&&(r.comments={_text:t.encodeXML(e.link)}),e.pubDate&&(r.pubDate={_text:e.pubDate.toUTCString()}),e.content&&(a=!0,r["content:encoded"]={_cdata:t.encodeCDATA(e.content)}),e.enclosure&&(r.enclosure={_attributes:{url:(o=e.enclosure).url,...o.length?{length:o.length}:{},...o.type?{type:o.type}:{}}}),r})),a&&(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:r,links:a}=e.options,o={version:"https://jsonfeed.org/version/1.1",title:r.title,home_page_url:r.link,feed_url:a.json};return r.description&&(o.description=r.description),r.image&&(o.icon=r.image),r.icon&&(o.favicon=r.icon),(null===(i=r.author)||void 0===i?void 0:i.name)&&(o.author={name:r.author.name,...r.author.url?{url:r.author.url}:{},...r.author.avatar?{avatar:r.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,r){this.app=t,this.options=e,this.page=i,this.feed=r,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.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.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 r=/!\[.*?\]\(.*?\)/iu.exec(this.page.content);if(r){if(t.isAbsoluteUrl(r[1]))return l(this.options.hostname,this.app.options.base,r[1]);if(t.isUrl(r[1]))return r[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:r,copyright:a,description:o,enclosure:n,guid:s,image:u,lastUpdated:l,link:p,pubDate:c,title:h}=this;return h||o?(e&&e.forEach((t=>this.feed.addCategory(t.name))),r&&r.forEach((t=>this.feed.addContributor(t))),{title:h,link:p,description:o,author:t,category:e,enclosure:n,guid:s,pubDate:c,lastUpdated:l,content:i,image:u,contributor:r,copyright:a}):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:d(t,i)})])))}addPages(t){const i=this.feedMap[t],r=this.options[t],{filter:a=(({frontmatter:t,filePathRelative:e})=>!(t.home||!e||!1===t.article||!1===t.feed)),sorter:o=((t,e)=>{var i,r,a,o;return s((null===(i=t.git)||void 0===i?void 0:i.createdTime)?new Date(null===(r=t.git)||void 0===r?void 0:r.createdTime):t.frontmatter.date,(null===(a=e.git)||void 0===a?void 0:a.createdTime)?new Date(null===(o=e.git)||void 0===o?void 0:o.createdTime):e.frontmatter.date)})}=r,u=this.app.pages.filter((e=>e.pathLocale===t)).filter(a).sort(o).slice(0,r.count||100);let l=0;for(const t of u){const e=new _(this.app,r,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([r,a])=>{if(a.atom||a.json||a.rss){const o=this.feedMap[r],{atomOutputFilename:s,jsonOutputFilename:u,rssOutputFilename:l}=h(a,r);this.addPages(r),a.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)}`)),a.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)}`)),a.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,r,a,o;return s((null===(i=t.git)||void 0===i?void 0:i.createdTime)?new Date(null===(r=t.git)||void 0===r?void 0:r.createdTime):t.frontmatter.date,(null===(a=e.git)||void 0===a?void 0:a.createdTime)?new Date(null===(o=e.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:r}=t,a=Object.keys(e);if(1===a.length){const{atomOutputFilename:t,jsonOutputFilename:a,rssOutputFilename:o}=h(e["/"]),n=(t,a,o)=>["link",{rel:"alternate",type:o,href:l(e["/"].hostname,i,a),title:`${r.title||r.locales["/"].title||""} ${t} Feed`}];r.head||(r.head=[]),e.atom&&r.head.push(n("Atom",t,"application/atom+xml")),e.json&&r.head.push(n("JSON",a,"application/json")),e.rss&&r.head.push(n("RSS",o,"application/rss+xml"))}else t.pages.forEach((t=>{const{pathLocale:o}=t,n=e[o];if(a.includes(o)){const{atomOutputFilename:e,jsonOutputFilename:a,rssOutputFilename:s}=h(n,o),u=(t,e,a)=>["link",{rel:"alternate",type:a,href:l(n.hostname,i,e),title:`${r.locales[o].title||r.title||r.locales["/"].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",a,"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/utils"),i=require("path"),r=require("xml-js");function a(t){if(t&&t.__esModule)return t;var e=Object.create(null);return t&&Object.keys(t).forEach((function(i){if("default"!==i){var r=Object.getOwnPropertyDescriptor(t,i);Object.defineProperty(e,i,r.get?r:{enumerable:!0,get:function(){return t[i]}})}})),e.default=t,Object.freeze(e)}var o=a(r);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,r="")=>{var a,o,n,s,u,p,c;const{hostname:h,icon:d,image:g}=i,{base:m}=e.options,f=null===(o=null===(a=i.channel)||void 0===a?void 0:a.author)||void 0===o?void 0:o.name,y={title:(null===(n=e.siteData.locales[r])||void 0===n?void 0:n.title)||e.siteData.title||(null===(s=e.siteData.locales["/"])||void 0===s?void 0:s.title)||"",link:l(h,m,r),description:(null===(u=e.siteData.locales[r])||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[r])||void 0===c?void 0:c.lang)||e.siteData.lang,copyright:f?`Copyright by ${f}`:"",pubDate:new Date,lastUpdated:new Date,...d?{icon:d}:{},...g?{image:g}:{},...f?{author:{name:f}}:{}};return t.deepAssign(y,i.channel||{})},h=(t,e="/")=>({atomOutputFilename:`${e.replace(/^\//,"")}${t.atomOutputFilename||"atom.xml"}`,jsonOutputFilename:`${e.replace(/^\//,"")}${t.jsonOutputFilename||"feed.json"}`,rssOutputFilename:`${e.replace(/^\//,"")}${t.rssOutputFilename||"rss.xml"}`}),d=(t,e)=>{const{base:i}=t.options,{hostname:r}=e,{atomOutputFilename:a,jsonOutputFilename:o,rssOutputFilename:n}=h(e);return{atom:l(r,i,a),json:l(r,i,o),rss:l(r,i,n)}},g=e=>{const{name:i,email:r,url:a}=e;return{name:i,...r?{email:r}:{},...a?{uri:t.encodeXML(a)}:{}}},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: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:g(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=>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})),r.js2xml(o,{compact:!0,ignoreComment:!0,spaces:2})})(this),this.rss=()=>(e=>{const{links:i,channel:r}=e.options;let a=!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:r.title},link:{_text:t.encodeXML(r.link)},description:{_text:r.description},language:{_text:t.encodeXML(r.language)},pubDate:{_text:r.pubDate?r.pubDate.toUTCString():(new Date).toUTCString()},lastBuildDate:{_text:r.lastUpdated?r.lastUpdated.toUTCString():(new Date).toUTCString()},generator:{_text:"vuepress-plugin-feed2"},docs:{_text:"https://validator.w3.org/feed/docs/rss2.html"}}}};return r.copyright&&(n.rss.channel.copyright={_text:r.copyright}),r.ttl&&(n.rss.channel.ttl={_text:r.ttl.toString()}),r.image&&(n.rss.channel.image={title:{_text:r.title},url:{_text:r.image},link:{_text:t.encodeXML(r.link)}}),n.rss.channel.category=Array.from(e.categories).map((t=>({_text:t}))),n.rss.channel.item=e.items.map((e=>{const r={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&&(r.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&&(r.author={_text:`${t.email} (${t.name})`})}else if("object"==typeof e.author){const{name:t,email:i}=e.author;i&&t&&(r.author={_text:`${i} (${t})`})}var o;return Array.isArray(e.category)?r.category=e.category.filter((t=>t.name)).map((t=>y(t))):"object"==typeof e.category&&e.category.name&&(r.category=[y(e.category)]),e.comments&&(r.comments={_text:t.encodeXML(e.link)}),e.pubDate&&(r.pubDate={_text:e.pubDate.toUTCString()}),e.content&&(a=!0,r["content:encoded"]={_cdata:t.encodeCDATA(e.content)}),e.enclosure&&(r.enclosure={_attributes:{url:(o=e.enclosure).url,...o.length?{length:o.length}:{},...o.type?{type:o.type}:{}}}),r})),a&&(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:r,links:a}=e.options,o={version:"https://jsonfeed.org/version/1.1",title:r.title,home_page_url:r.link,feed_url:a.json};return r.description&&(o.description=r.description),r.image&&(o.icon=r.image),r.icon&&(o.favicon=r.icon),(null===(i=r.author)||void 0===i?void 0:i.name)&&(o.author={name:r.author.name,...r.author.url?{url:r.author.url}:{},...r.author.avatar?{avatar:r.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,r){this.app=t,this.options=e,this.page=i,this.feed=r,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.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.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 r=/!\[.*?\]\((.*?)\)/iu.exec(this.page.content);if(r){if(t.isAbsoluteUrl(r[1]))return l(this.options.hostname,this.app.options.base,r[1]);if(t.isUrl(r[1]))return r[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:r,copyright:a,description:o,enclosure:n,guid:s,image:u,lastUpdated:l,link:p,pubDate:c,title:h}=this;return h||o?(e&&e.forEach((t=>this.feed.addCategory(t.name))),r&&r.forEach((t=>this.feed.addContributor(t))),{title:h,link:p,description:o,author:t,category:e,enclosure:n,guid:s,pubDate:c,lastUpdated:l,content:i,image:u,contributor:r,copyright:a}):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:d(t,i)})])))}addPages(t){const i=this.feedMap[t],r=this.options[t],{filter:a=(({frontmatter:t,filePathRelative:e})=>!(t.home||!e||!1===t.article||!1===t.feed)),sorter:o=((t,e)=>{var i,r,a,o;return s((null===(i=t.git)||void 0===i?void 0:i.createdTime)?new Date(null===(r=t.git)||void 0===r?void 0:r.createdTime):t.frontmatter.date,(null===(a=e.git)||void 0===a?void 0:a.createdTime)?new Date(null===(o=e.git)||void 0===o?void 0:o.createdTime):e.frontmatter.date)})}=r,u=this.app.pages.filter((e=>e.pathLocale===t)).filter(a).sort(o).slice(0,r.count||100);let l=0;for(const t of u){const e=new _(this.app,r,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([r,a])=>{if(a.atom||a.json||a.rss){const o=this.feedMap[r],{atomOutputFilename:s,jsonOutputFilename:u,rssOutputFilename:l}=h(a,r);this.addPages(r),a.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)}`)),a.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)}`)),a.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,r,a,o;return s((null===(i=t.git)||void 0===i?void 0:i.createdTime)?new Date(null===(r=t.git)||void 0===r?void 0:r.createdTime):t.frontmatter.date,(null===(a=e.git)||void 0===a?void 0:a.createdTime)?new Date(null===(o=e.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:r}=t,a=Object.keys(e);if(1===a.length){const{atomOutputFilename:t,jsonOutputFilename:a,rssOutputFilename:o}=h(e["/"]),n=(t,a,o)=>["link",{rel:"alternate",type:o,href:l(e["/"].hostname,i,a),title:`${r.title||r.locales["/"].title||""} ${t} Feed`}];r.head||(r.head=[]),e.atom&&r.head.push(n("Atom",t,"application/atom+xml")),e.json&&r.head.push(n("JSON",a,"application/json")),e.rss&&r.head.push(n("RSS",o,"application/rss+xml"))}else t.pages.forEach((t=>{const{pathLocale:o}=t,n=e[o];if(a.includes(o)){const{atomOutputFilename:e,jsonOutputFilename:a,rssOutputFilename:s}=h(n,o),u=(t,e,a)=>["link",{rel:"alternate",type:a,href:l(n.hostname,i,e),title:`${r.locales[o].title||r.title||r.locales["/"].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",a,"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; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "vuepress-plugin-feed2", | ||
"version": "2.0.0-beta.8", | ||
"version": "2.0.0-beta.9", | ||
"description": "Feed plugin for vuepress-theme-hope", | ||
@@ -44,3 +44,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@mr-hope/vuepress-shared": "2.0.0-beta.8", | ||
"@mr-hope/vuepress-shared": "2.0.0-beta.9", | ||
"@vuepress/utils": "2.0.0-beta.35", | ||
@@ -55,3 +55,3 @@ "xml-js": "^1.6.11" | ||
}, | ||
"gitHead": "646fe86ed1406a1a3f093961289e325c6b1b87f9" | ||
"gitHead": "640f93310741bc440327a0f1370fe48b5544853f" | ||
} |
@@ -178,3 +178,3 @@ import { | ||
const result = /!\[.*?\]\(.*?\)/iu.exec(this.page.content); | ||
const result = /!\[.*?\]\((.*?)\)/iu.exec(this.page.content); | ||
@@ -181,0 +181,0 @@ if (result) { |
Sorry, the diff of this file is not supported yet
102015
+ Added@mr-hope/vuepress-shared@2.0.0-beta.9(transitive)
- Removed@mr-hope/vuepress-shared@2.0.0-beta.8(transitive)