@vuepress/plugin-feed
Advanced tools
Comparing version 2.0.0-rc.66 to 2.0.0-rc.68
@@ -1,2 +0,2 @@ | ||
import{fromEntries as D,entries as b,isArray as g,isPlainObject as F,isString as S,Logger as Z,keys as R,isFunction as m,getPageText as tt,getPageExcerpt as N,isLinkAbsolute as U,isLinkWithProtocol as $,dateSorter as et,values as it,customizeDevServer as st}from"@vuepress/helper";import{isLinkHttp as _,removeEndingSlash as X,removeLeadingSlash as x,isString as at,ensureEndingSlash as rt}from"vuepress/shared";import{colors as P,getDirname as nt,path as J,fs as w}from"vuepress/utils";import{js2xml as M}from"xml-js";const ot=e=>e.replace(/]]>/g,"]]]]><![CDATA[>"),L=e=>e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'"),j=e=>D(b(e).map(([t,a])=>t==="_attributes"&&a?[t,D(b(a).map(([s,i])=>[s,i?L(i.toString()):void 0]))]:t==="_text"?[t,L(a.toString())]:t==="_cdata"?[t,ot(a)]:t==="_comment"||t==="_instruction"?[t,a]:g(a)?[t,a.map(s=>j(s))]:F(a)?[t,j(a)]:[t,L(String(a))])),q=e=>F(e)&&S(e.name),B=e=>e?g(e)?e.map(t=>S(t)?{name:t}:q(t)?t:null).filter(t=>t!==null):S(e)?[{name:e}]:q(e)?[e]:(console.error('Expect "author" to be `AuthorInfo[] | AuthorInfo | string[] | string | undefined`, but got',e),[]):[],lt=e=>{if(e){if(g(e)&&e.every(S))return e;if(S(e))return[e]}return[]},pt=(e="")=>`image/${e==="jpg"?"jpeg":e==="svg"?"svg+xml":e==="jpeg"||e==="png"||e==="bmp"||e==="gif"||e==="webp"?e:""}`,h=(e,t="",a="")=>`${_(e)?X(e):`https://${X(e)}`}${t}${x(a)}`,C="@vuepress/plugin-feed",T=new Z(C),O=(e,t="/")=>({atomOutputFilename:`${x(t)}${e.atomOutputFilename||"atom.xml"}`,atomXslFilename:`${x(t)}${e.atomXslFilename||"atom.xsl"}`,jsonOutputFilename:`${x(t)}${e.jsonOutputFilename||"feed.json"}`,rssOutputFilename:`${x(t)}${e.rssOutputFilename||"rss.xml"}`,rssXslFilename:`${x(t)}${e.rssXslFilename||"rss.xsl"}`}),ct=(e,t)=>{const{base:a}=e.options,{siteData:s}=e,i=R(t);if(i.length===1){const{atomOutputFilename:r,jsonOutputFilename:n,rssOutputFilename:o}=O(t["/"]),{atom:l,json:p,rss:y,hostname:u}=t["/"],c=(d,f,E)=>["link",{rel:"alternate",type:E,href:h(u,a,f),title:`${s.title||s.locales["/"]?.title||""} ${d} Feed`}];l&&s.head.push(c("Atom",r,"application/atom+xml")),p&&s.head.push(c("JSON",n,"application/json")),y&&s.head.push(c("RSS",o,"application/rss+xml"))}else e.pages.forEach(r=>{const{pathLocale:n}=r,o=t[n];if(i.includes(n)){const{atomOutputFilename:l,jsonOutputFilename:p,rssOutputFilename:y}=O(o,n),u=(c,d,f)=>["link",{rel:"alternate",type:f,href:h(o.hostname,a,d),title:`${s.locales[n]?.title||s.title||s.locales["/"]?.title||""} ${c} Feed`}];r.frontmatter.head??=[],o.atom&&r.frontmatter.head.push(u("Atom",l,"application/atom+xml")),o.json&&r.frontmatter.head.push(u("JSON",p,"application/json")),o.rss&&r.frontmatter.head.push(u("RSS",y,"application/rss+xml"))}})};class ht{constructor(t,a,s,i){this.app=t,this.options=a,this.page=s,this.hostname=i,this.base=this.app.options.base,this.frontmatter=s.frontmatter,this.getter=a.getter??{},this.pageOptions=this.frontmatter.feed||{}}pageOptions;frontmatter;base;getter;get title(){return m(this.getter.title)?this.getter.title(this.page,this.app):this.pageOptions.title||this.page.title}get link(){return m(this.getter.link)?this.getter.link(this.page,this.app):h(this.hostname,this.base,this.page.path)}get description(){if(m(this.getter.description))return this.getter.description(this.page,this.app);if(this.pageOptions.description)return this.pageOptions.description;if(this.frontmatter.description)return this.frontmatter.description;const t=tt(this.app,this.page,{length:180});return t.length>180?`${t.slice(0,177)}...`:t}get guid(){return this.pageOptions.guid||this.link}get author(){return m(this.getter.author)?this.getter.author(this.page,this.app):g(this.pageOptions.author)?this.pageOptions.author:F(this.pageOptions.author)?[this.pageOptions.author]:this.frontmatter.author?B(this.frontmatter.author):this.options.channel?.author?B(this.options.channel.author):[]}get category(){if(m(this.getter.category))return this.getter.category(this.page,this.app);if(g(this.pageOptions.category))return this.pageOptions.category;if(F(this.pageOptions.category))return[this.pageOptions.category];const{categories:t,category:a=t}=this.frontmatter;return lt(a).map(s=>({name:s}))}get enclosure(){return m(this.getter.enclosure)?this.getter.enclosure(this.page,this.app):this.image?{url:this.image,type:pt(this.image.split(".").pop())}:null}get pubDate(){if(m(this.getter.publishDate))return this.getter.publishDate(this.page,this.app);const{time:t,date:a=t}=this.page.frontmatter,{createdTime:s}=this.page.data.git??{};return a&&a instanceof Date?a:s?new Date(s):null}get lastUpdated(){if(m(this.getter.lastUpdateDate))return this.getter.lastUpdateDate(this.page,this.app);const{updatedTime:t}=this.page.data.git??{};return t?new Date(t):null}get summary(){return m(this.getter.excerpt)?this.getter.excerpt(this.page,this.app):this.pageOptions.summary?this.pageOptions.summary:N(this.app,this.page,{isCustomElement:this.options.isPreservedElement})}get content(){return m(this.getter.content)?this.getter.content(this.page,this.app):this.pageOptions.content?this.pageOptions.content:N(this.app,this.page,{isCustomElement:this.options.isPreservedElement,separator:"",length:1/0})}get image(){if(m(this.getter.image))return this.getter.image(this.page,this.app);const{hostname:t,base:a}=this,{banner:s,cover:i}=this.frontmatter;if(s){if(U(s))return h(t,a,s);if($(s))return s}if(i){if(U(i))return h(t,a,i);if($(i))return i}const r=/!\[.*?\]\((.*?)\)/iu.exec(this.page.content);if(r){if(U(r[1]))return h(t,a,r[1]);if($(r[1]))return r[1]}return null}get contributor(){return m(this.getter.contributor)?this.getter.contributor(this.page,this.app):g(this.pageOptions.contributor)?this.pageOptions.contributor:F(this.pageOptions.contributor)?[this.pageOptions.contributor]:this.author}get copyright(){if(m(this.getter.copyright))return this.getter.copyright(this.page,this.app);if(at(this.frontmatter.copyright))return this.frontmatter.copyright;const t=this.author[0];return t?.name?`Copyright by ${t.name}`:null}get isValid(){return!!(this.title||this.description)}}const mt=(e,t,a="")=>{const{base:s}=e.options,{title:i,description:r,lang:n,locales:o}=e.siteData,{channel:{icon:l,image:p,...y}={},hostname:u,icon:c,image:d}=t,f=g(t.channel?.author)?t.channel.author[0]?.name:t.channel?.author?.name;return{title:o[a]?.title||i||o["/"]?.title||"",link:h(u,s,a),description:o[a]?.description||r||o["/"]?.description||"",language:o[a]?.lang||n,copyright:f?`Copyright by ${f}`:"",pubDate:new Date,lastUpdated:new Date,...c?{icon:_(c)?c:h(u,s,c)}:{},...d?{image:_(d)?d:h(u,s,d)}:{},...y,...l?{icon:_(l)?l:h(u,s,l)}:{},...p?{image:_(p)?p:h(u,s,p)}:{}}},ut=(e,t,a)=>{const{base:s}=e.options,{hostname:i}=t,{atomOutputFilename:r,atomXslFilename:n,jsonOutputFilename:o,rssOutputFilename:l,rssXslFilename:p}=O(t,a);return{localePath:a,atom:h(i,s,r),atomXsl:h(i,s,n),json:h(i,s,o),rss:h(i,s,l),rssXsl:h(i,s,p)}};class gt{categories=new Set;contributors=[];items=[];contributorKeys=new Set;channel;links;constructor(t,a,s){this.channel=mt(t,a,s),this.links=ut(t,a,s)}addCategory=t=>{this.categories.add(t.name)};addContributor=t=>{const a=t.email||t.name;a&&!this.contributorKeys.has(a)&&(this.contributorKeys.add(a),this.contributors.push(t))};add=t=>{if(t.isValid){const{category:a,contributor:s}=t;this.items.push(t),a?.forEach(this.addCategory),s.forEach(this.addContributor)}}}const v=e=>{const{name:t="Unknown",email:a,url:s}=e;return{name:t,...a?{email:a}:{},...s?{uri:s}:{}}},dt=e=>{const{name:t,scheme:a=""}=e;return{_attributes:{term:t,scheme:a}}},ft=e=>{const{channel:t,links:a}=e,s={_declaration:{_attributes:{version:"1.0",encoding:"utf-8"}},_instruction:{"xml-stylesheet":`type="text/xsl" href="${a.atomXsl}"`},feed:{_attributes:{xmlns:"http://www.w3.org/2005/Atom",...t.language?{"xml:lang":t.language}:{}},id:t.link,title:t.title,...t.description?{subtitle:t.description}:{},...t.author?{author:g(t.author)?t.author.map(i=>v(i)):[v(t.author)]}:{},...t.icon?{icon:t.icon}:{},...t.image?{logo:t.image}:{},...t.copyright?{rights:t.copyright}:{},updated:t.lastUpdated?t.lastUpdated.toISOString():new Date().toISOString(),generator:C,link:[{_attributes:{rel:"self",href:a.atom}}]}};return t.link&&s.feed.link.push({_attributes:{rel:"alternate",href:t.link}}),t.hub&&s.feed.link.push({_attributes:{rel:"hub",href:t.hub}}),t.image&&(s.feed.logo=t.image),t.icon&&(s.feed.icon=t.icon),t.copyright&&(s.feed.rights=t.copyright),s.feed.category=Array.from(e.categories).map(i=>({_attributes:{term:i}})),s.feed.contributor=Array.from(e.contributors).filter(i=>i.name).map(i=>v(i)),s.feed.entry=e.items.map(i=>{const r={title:{_attributes:{type:"text"},_text:i.title},id:i.guid||i.link,link:{_attributes:{href:i.link}},updated:(i.lastUpdated??new Date).toISOString()};return i.summary?r.summary={_attributes:{type:"html"},_cdata:i.summary}:i.description&&(r.summary={_attributes:{type:"text"},_text:i.description}),i.content&&(r.content={_attributes:{type:"html"},_cdata:i.content}),r.author=i.author.filter(n=>n.name).map(n=>v(n)),i.category&&(r.category=i.category.map(n=>dt(n))),i.contributor.length&&(r.contributor=i.contributor.map(n=>v(n))),i.pubDate&&(r.published=i.pubDate.toISOString()),i.copyright&&(r.rights=i.copyright),r}),M(j(s),{compact:!0,ignoreComment:!0,spaces:2})},H=e=>({name:e.name,...e.url?{url:e.url}:{},...e.avatar?{avatar:e.avatar}:{}}),yt=e=>{const{channel:t,links:a}=e,s={version:"https://jsonfeed.org/version/1.1",title:t.title,home_page_url:t.link,feed_url:a.json};t.description&&(s.description=t.description),t.image&&(s.icon=t.image),t.icon&&(s.favicon=t.icon);const i=(g(t.author)?t.author:t.author?[t.author]:[]).filter(r=>r?.name);return i.length&&(s.authors=i.map(r=>H(r))),s.items=e.items.map(r=>{const n={title:r.title,url:r.link,id:r.guid||r.link,...r.description?{summary:r.description}:{},content_html:r.content};return r.image&&(n.image=r.image),r.pubDate&&(n.date_published=r.pubDate.toISOString()),r.lastUpdated&&(n.date_modified=r.lastUpdated.toISOString()),g(r.author)&&(n.authors=r.author.filter(o=>o.name).map(o=>H(o))),r.category&&(n.tags=r.category.filter(o=>o.name).map(o=>o.name)),n}),JSON.stringify(s,null,2)},bt=e=>{const{name:t,domain:a}=e;return{_text:t,...a?{_attributes:{domain:a}}:{}}},_t=e=>{const t=e.guid||e.link;return{...$(t)?{}:{_attributes:{isPermaLink:"false"}},_text:t}},xt=e=>({_attributes:{url:e.url,...e.length?{length:e.length}:{},...e.type?{type:e.type}:{}}}),Ot=e=>{const{channel:t,links:a}=e;let s=!1;const i={_declaration:{_attributes:{version:"1.0",encoding:"utf-8"}},_instruction:{"xml-stylesheet":`type="text/xsl" href="${a.rssXsl}"`},rss:{_attributes:{version:"2.0","xmlns:atom":"http://www.w3.org/2005/Atom"},channel:{"atom:link":{_attributes:{href:a.rss,rel:"self",type:"application/rss+xml"}},title:{_text:t.title},link:{_text:t.link},description:{_text:t.description},language:{_text:t.language},pubDate:{_text:t.pubDate?t.pubDate.toUTCString():new Date().toUTCString()},lastBuildDate:{_text:t.lastUpdated?t.lastUpdated.toUTCString():new Date().toUTCString()},generator:{_text:C},docs:{_text:"https://validator.w3.org/feed/docs/rss2.html"}}}};return t.copyright&&(i.rss.channel.copyright={_text:t.copyright}),t.ttl&&(i.rss.channel.ttl={_text:t.ttl.toString()}),t.image&&(i.rss.channel.image={title:{_text:t.title},url:{_text:t.image},link:{_text:t.link}}),i.rss.channel.category=Array.from(e.categories).map(r=>({_text:r})),i.rss.channel.item=e.items.map(r=>{const n={title:{_text:r.title},link:{_text:r.link},guid:_t(r),source:{_attributes:{url:a.rss},_text:r.title}};r.description&&(n.description={_text:r.description});const o=r.author.find(l=>l.email&&l.name);return o&&(n.author={_text:`${o.email} (${o.name})`}),r.category&&(n.category=r.category.filter(l=>l.name).map(l=>bt(l))),r.pubDate&&(n.pubDate={_text:r.pubDate.toUTCString()}),r.content&&(s=!0,n["content:encoded"]={_cdata:r.content}),r.enclosure&&(n.enclosure=xt(r.enclosure)),n}),s&&(i.rss._attributes["xmlns:content"]="http://purl.org/rss/1.0/modules/content/",i.rss._attributes["xmlns:dc"]="http://purl.org/dc/elements/1.1/"),M(j(i),{compact:!0,ignoreComment:!0,spaces:2})},K=(e,t,a)=>{const s=D(b(t).map(([i,r])=>[i,new gt(e,r,i)]));return b(t).filter(([,{atom:i,json:r,rss:n}])=>i||r||n).map(([i,r])=>{const{atom:n,json:o,rss:l,count:p=100,filter:y,sorter:u}=r,c=s[i],d=e.pages.filter(A=>A.pathLocale===i).filter(y).sort(u);for(const A of d){const Y=new ht(e,r,A,a);if(c.add(Y),c.items.length===p)break}const f=c.items.length;T.succeed(`added ${P.cyan(`${f} page${f>1?"s":""}`)} as feed item${f>1?"s":""} in locale ${P.cyan(i)}`);const{atomOutputFilename:E,jsonOutputFilename:W,rssOutputFilename:Q}=O(r,i),k=[];return n&&k.push([E,ft(c),"application/atom+xml"]),o&&k.push([W,yt(c),"application/json"]),l&&k.push([Q,Ot(c),"application/xml"]),k}).flat()},Ft=({siteData:e},t)=>D(R({"/":{},...e.locales}).map(a=>{const s=t.locales?.[a]?.preservedElements??t.preservedElements,{hostname:i,devServer:r,locales:n,...o}=t;return[a,{filter:({frontmatter:l,filePathRelative:p})=>!!(l.feed??(p&&!l.home)),sorter:(l,p)=>et(l.data.git?.createdTime?new Date(l.data.git.createdTime):l.frontmatter.date,p.data.git?.createdTime?new Date(p.data.git.createdTime):p.frontmatter.date),...o,...t.locales?.[a],hostname:i,isPreservedElement:g(s)?l=>s.some(p=>p instanceof RegExp?p.test(l):p===l):m(s)?s:()=>!1}]})),St=import.meta.dirname||nt(import.meta.url),z=rt(J.resolve(St,"../../templates")),vt=w.readFileSync(`${z}atom.xsl`,"utf-8"),kt=w.readFileSync(`${z}rss.xsl`,"utf-8"),G=e=>b(e).filter(([,{atom:t}])=>t).map(([t,a])=>{const{atomXslTemplate:s=vt}=a,{atomXslFilename:i}=O(a,t);return[i,s]}),V=e=>b(e).filter(([,{rss:t}])=>t).map(([t,a])=>{const{rssXslTemplate:s=kt}=a,{rssXslFilename:i}=O(a,t);return[i,s]}),I=(e,t)=>t.map(async([a,s])=>{const i=e.dir.dest(a);await w.ensureDir(J.dirname(i)),await w.writeFile(i,s,"utf-8")}),Dt=e=>t=>{t.env.isDebug&&T.info("Options:",e);const a={name:C};let s=t.env.isDev?e.devHostname||`http://localhost:${t.options.port}`:e.hostname;if(!s)return T.error(`Option ${P.magenta("hostname")} is required!`),a;if(s=X(_(s)?s:`https://${s}`),!e.atom&&!e.json&&!e.rss&&e.locales&&it(e.locales).every(({atom:r,json:n,rss:o})=>!r&&!n&&!o))return T.info("No feed output requested, the plugin won’t start!"),a;const i=Ft(t,e);return{...a,onInitialized:()=>{(t.env.isBuild||e.devServer)&&ct(t,i)},extendsBundlerOptions:r=>{e.devServer&&[...K(t,i,s),...G(i),...V(i)].forEach(([n,o,l])=>{st(r,t,{path:n,response:(p,y)=>(l&&y.setHeader("Content-Type",l),Promise.resolve(o)),errMsg:"Unexpected feed generation error"})})},onGenerated:async()=>{await Promise.all([...I(t,K(t,i,s)),...I(t,G(i)),...I(t,V(i))])}}};export{Dt as feedPlugin}; | ||
import{fromEntries as w,entries as b,isArray as f,isPlainObject as v,isString as k,Logger as Z,keys as J,isFunction as u,getPageText as tt,getPageExcerpt as M,isLinkAbsolute as P,isLinkWithProtocol as j,dateSorter as et,values as it,customizeDevServer as st}from"@vuepress/helper";import{isLinkHttp as _,removeEndingSlash as L,removeLeadingSlash as x,isString as at,ensureEndingSlash as rt}from"vuepress/shared";import{colors as I,getDirname as nt,path as q,fs as C}from"vuepress/utils";import{js2xml as B}from"xml-js";const ot=e=>e.replace(/]]>/g,"]]]]><![CDATA[>"),R=e=>e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'"),T=e=>w(b(e).map(([t,a])=>t==="_attributes"&&a?[t,w(b(a).map(([s,i])=>[s,i?R(i.toString()):void 0]))]:t==="_text"?[t,R(a.toString())]:t==="_cdata"?[t,ot(a)]:t==="_comment"||t==="_instruction"?[t,a]:f(a)?[t,a.map(s=>T(s))]:v(a)?[t,T(a)]:[t,R(String(a))])),H=e=>v(e)&&k(e.name),K=e=>e?f(e)?e.map(t=>k(t)?{name:t}:H(t)?t:null).filter(t=>t!==null):k(e)?[{name:e}]:H(e)?[e]:(console.error('Expect "author" to be `AuthorInfo[] | AuthorInfo | string[] | string | undefined`, but got',e),[]):[],lt=e=>{if(e){if(f(e)&&e.every(k))return e;if(k(e))return[e]}return[]},pt=(e="")=>`image/${e==="jpg"?"jpeg":e==="svg"?"svg+xml":e==="jpeg"||e==="png"||e==="bmp"||e==="gif"||e==="webp"?e:""}`,c=(e,t="",a="")=>`${_(e)?L(e):`https://${L(e)}`}${t}${x(a)}`,E="@vuepress/plugin-feed",A=new Z(E),O=(e,t="/")=>({atomOutputFilename:`${x(t)}${e.atomOutputFilename||"atom.xml"}`,atomXslFilename:`${x(t)}${e.atomXslFilename||"atom.xsl"}`,jsonOutputFilename:`${x(t)}${e.jsonOutputFilename||"feed.json"}`,rssOutputFilename:`${x(t)}${e.rssOutputFilename||"rss.xml"}`,rssXslFilename:`${x(t)}${e.rssXslFilename||"rss.xsl"}`}),ct=(e,t)=>{const{base:a,title:s,locales:i,head:r}=e.siteData,n=J(t);if(n.length===1){const{atomOutputFilename:o,jsonOutputFilename:l,rssOutputFilename:p}=O(t["/"]),{atom:y,json:d,rss:m,hostname:h}=t["/"],g=(F,S,U)=>["link",{rel:"alternate",type:U,href:c(h,a,S),title:`${s||i["/"]?.title||""} ${F} Feed`}];y&&r.push(g("Atom",o,"application/atom+xml")),d&&r.push(g("JSON",l,"application/json")),m&&r.push(g("RSS",p,"application/rss+xml"))}else e.pages.forEach(o=>{const{pathLocale:l}=o,p=t[l];if(n.includes(l)){const{atomOutputFilename:y,jsonOutputFilename:d,rssOutputFilename:m}=O(p,l),h=(g,F,S)=>["link",{rel:"alternate",type:S,href:c(p.hostname,a,F),title:`${i[l]?.title||s||i["/"]?.title||""} ${g} Feed`}];o.frontmatter.head??=[],p.atom&&o.frontmatter.head.push(h("Atom",y,"application/atom+xml")),p.json&&o.frontmatter.head.push(h("JSON",d,"application/json")),p.rss&&o.frontmatter.head.push(h("RSS",m,"application/rss+xml"))}})};class mt{constructor(t,a,s,i){this.app=t,this.options=a,this.page=s,this.hostname=i,this.base=this.app.siteData.base,this.frontmatter=s.frontmatter,this.getter=a.getter??{},this.pageOptions=this.frontmatter.feed||{}}pageOptions;frontmatter;base;getter;get title(){return u(this.getter.title)?this.getter.title(this.page,this.app):this.pageOptions.title||this.page.title}get link(){return u(this.getter.link)?this.getter.link(this.page,this.app):c(this.hostname,this.base,this.page.path)}get description(){if(u(this.getter.description))return this.getter.description(this.page,this.app);if(this.pageOptions.description)return this.pageOptions.description;if(this.frontmatter.description)return this.frontmatter.description;const t=tt(this.app,this.page,{length:180});return t.length>180?`${t.slice(0,177)}...`:t}get guid(){return this.pageOptions.guid||this.link}get author(){return u(this.getter.author)?this.getter.author(this.page,this.app):f(this.pageOptions.author)?this.pageOptions.author:v(this.pageOptions.author)?[this.pageOptions.author]:this.frontmatter.author?K(this.frontmatter.author):this.options.channel?.author?K(this.options.channel.author):[]}get category(){if(u(this.getter.category))return this.getter.category(this.page,this.app);if(f(this.pageOptions.category))return this.pageOptions.category;if(v(this.pageOptions.category))return[this.pageOptions.category];const{categories:t,category:a=t}=this.frontmatter;return lt(a).map(s=>({name:s}))}get enclosure(){return u(this.getter.enclosure)?this.getter.enclosure(this.page,this.app):this.image?{url:this.image,type:pt(this.image.split(".").pop())}:null}get pubDate(){if(u(this.getter.publishDate))return this.getter.publishDate(this.page,this.app);const{time:t,date:a=t}=this.page.frontmatter,{createdTime:s}=this.page.data.git??{};return a&&a instanceof Date?a:s?new Date(s):null}get lastUpdated(){if(u(this.getter.lastUpdateDate))return this.getter.lastUpdateDate(this.page,this.app);const{updatedTime:t}=this.page.data.git??{};return t?new Date(t):null}get summary(){return u(this.getter.excerpt)?this.getter.excerpt(this.page,this.app):this.pageOptions.summary?this.pageOptions.summary:M(this.app,this.page,{isCustomElement:this.options.isPreservedElement})}get content(){return u(this.getter.content)?this.getter.content(this.page,this.app):this.pageOptions.content?this.pageOptions.content:M(this.app,this.page,{isCustomElement:this.options.isPreservedElement,separator:"",length:1/0})}get image(){if(u(this.getter.image))return this.getter.image(this.page,this.app);const{hostname:t,base:a}=this,{banner:s,cover:i}=this.frontmatter;if(s){if(P(s))return c(t,a,s);if(j(s))return s}if(i){if(P(i))return c(t,a,i);if(j(i))return i}const r=/!\[.*?\]\((.*?)\)/iu.exec(this.page.content);if(r){if(P(r[1]))return c(t,a,r[1]);if(j(r[1]))return r[1]}return null}get contributor(){return u(this.getter.contributor)?this.getter.contributor(this.page,this.app):f(this.pageOptions.contributor)?this.pageOptions.contributor:v(this.pageOptions.contributor)?[this.pageOptions.contributor]:this.author}get copyright(){if(u(this.getter.copyright))return this.getter.copyright(this.page,this.app);if(at(this.frontmatter.copyright))return this.frontmatter.copyright;const t=this.author[0];return t?.name?`Copyright by ${t.name}`:null}get isValid(){return!!(this.title||this.description)}}const ut=(e,t,a="")=>{const{base:s,title:i,description:r,lang:n,locales:o}=e.siteData,{channel:{icon:l,image:p,...y}={},hostname:d,icon:m,image:h}=t,g=f(t.channel?.author)?t.channel.author[0]?.name:t.channel?.author?.name;return{title:o[a]?.title||i||o["/"]?.title||"",link:c(d,s,a),description:o[a]?.description||r||o["/"]?.description||"",language:o[a]?.lang||n,copyright:g?`Copyright by ${g}`:"",pubDate:new Date,lastUpdated:new Date,...m?{icon:_(m)?m:c(d,s,m)}:{},...h?{image:_(h)?h:c(d,s,h)}:{},...y,...l?{icon:_(l)?l:c(d,s,l)}:{},...p?{image:_(p)?p:c(d,s,p)}:{}}},ht=(e,t,a)=>{const{base:s}=e.siteData,{hostname:i}=t,{atomOutputFilename:r,atomXslFilename:n,jsonOutputFilename:o,rssOutputFilename:l,rssXslFilename:p}=O(t,a);return{localePath:a,atom:c(i,s,r),atomXsl:c(i,s,n),json:c(i,s,o),rss:c(i,s,l),rssXsl:c(i,s,p)}};class gt{categories=new Set;contributors=[];items=[];contributorKeys=new Set;channel;links;constructor(t,a,s){this.channel=ut(t,a,s),this.links=ht(t,a,s)}addCategory=t=>{this.categories.add(t.name)};addContributor=t=>{const a=t.email||t.name;a&&!this.contributorKeys.has(a)&&(this.contributorKeys.add(a),this.contributors.push(t))};add=t=>{if(t.isValid){const{category:a,contributor:s}=t;this.items.push(t),a?.forEach(this.addCategory),s.forEach(this.addContributor)}}}const D=e=>{const{name:t="Unknown",email:a,url:s}=e;return{name:t,...a?{email:a}:{},...s?{uri:s}:{}}},dt=e=>{const{name:t,scheme:a=""}=e;return{_attributes:{term:t,scheme:a}}},ft=e=>{const{channel:t,links:a}=e,s={_declaration:{_attributes:{version:"1.0",encoding:"utf-8"}},_instruction:{"xml-stylesheet":`type="text/xsl" href="${a.atomXsl}"`},feed:{_attributes:{xmlns:"http://www.w3.org/2005/Atom",...t.language?{"xml:lang":t.language}:{}},id:t.link,title:t.title,...t.description?{subtitle:t.description}:{},...t.author?{author:f(t.author)?t.author.map(i=>D(i)):[D(t.author)]}:{},...t.icon?{icon:t.icon}:{},...t.image?{logo:t.image}:{},...t.copyright?{rights:t.copyright}:{},updated:t.lastUpdated?t.lastUpdated.toISOString():new Date().toISOString(),generator:E,link:[{_attributes:{rel:"self",href:a.atom}}]}};return t.link&&s.feed.link.push({_attributes:{rel:"alternate",href:t.link}}),t.hub&&s.feed.link.push({_attributes:{rel:"hub",href:t.hub}}),t.image&&(s.feed.logo=t.image),t.icon&&(s.feed.icon=t.icon),t.copyright&&(s.feed.rights=t.copyright),s.feed.category=Array.from(e.categories).map(i=>({_attributes:{term:i}})),s.feed.contributor=Array.from(e.contributors).filter(i=>i.name).map(i=>D(i)),s.feed.entry=e.items.map(i=>{const r={title:{_attributes:{type:"text"},_text:i.title},id:i.guid||i.link,link:{_attributes:{href:i.link}},updated:(i.lastUpdated??new Date).toISOString()};return i.summary?r.summary={_attributes:{type:"html"},_cdata:i.summary}:i.description&&(r.summary={_attributes:{type:"text"},_text:i.description}),i.content&&(r.content={_attributes:{type:"html"},_cdata:i.content}),r.author=i.author.filter(n=>n.name).map(n=>D(n)),i.category&&(r.category=i.category.map(n=>dt(n))),i.contributor.length&&(r.contributor=i.contributor.map(n=>D(n))),i.pubDate&&(r.published=i.pubDate.toISOString()),i.copyright&&(r.rights=i.copyright),r}),B(T(s),{compact:!0,ignoreComment:!0,spaces:2})},z=e=>({name:e.name,...e.url?{url:e.url}:{},...e.avatar?{avatar:e.avatar}:{}}),yt=e=>{const{channel:t,links:a}=e,s={version:"https://jsonfeed.org/version/1.1",title:t.title,home_page_url:t.link,feed_url:a.json};t.description&&(s.description=t.description),t.image&&(s.icon=t.image),t.icon&&(s.favicon=t.icon);const i=(f(t.author)?t.author:t.author?[t.author]:[]).filter(r=>r?.name);return i.length&&(s.authors=i.map(r=>z(r))),s.items=e.items.map(r=>{const n={title:r.title,url:r.link,id:r.guid||r.link,...r.description?{summary:r.description}:{},content_html:r.content};return r.image&&(n.image=r.image),r.pubDate&&(n.date_published=r.pubDate.toISOString()),r.lastUpdated&&(n.date_modified=r.lastUpdated.toISOString()),f(r.author)&&(n.authors=r.author.filter(o=>o.name).map(o=>z(o))),r.category&&(n.tags=r.category.filter(o=>o.name).map(o=>o.name)),n}),JSON.stringify(s,null,2)},bt=e=>{const{name:t,domain:a}=e;return{_text:t,...a?{_attributes:{domain:a}}:{}}},_t=e=>{const t=e.guid||e.link;return{...j(t)?{}:{_attributes:{isPermaLink:"false"}},_text:t}},xt=e=>({_attributes:{url:e.url,...e.length?{length:e.length}:{},...e.type?{type:e.type}:{}}}),Ot=e=>{const{channel:t,links:a}=e;let s=!1;const i={_declaration:{_attributes:{version:"1.0",encoding:"utf-8"}},_instruction:{"xml-stylesheet":`type="text/xsl" href="${a.rssXsl}"`},rss:{_attributes:{version:"2.0","xmlns:atom":"http://www.w3.org/2005/Atom"},channel:{"atom:link":{_attributes:{href:a.rss,rel:"self",type:"application/rss+xml"}},title:{_text:t.title},link:{_text:t.link},description:{_text:t.description},language:{_text:t.language},pubDate:{_text:t.pubDate?t.pubDate.toUTCString():new Date().toUTCString()},lastBuildDate:{_text:t.lastUpdated?t.lastUpdated.toUTCString():new Date().toUTCString()},generator:{_text:E},docs:{_text:"https://validator.w3.org/feed/docs/rss2.html"}}}};return t.copyright&&(i.rss.channel.copyright={_text:t.copyright}),t.ttl&&(i.rss.channel.ttl={_text:t.ttl.toString()}),t.image&&(i.rss.channel.image={title:{_text:t.title},url:{_text:t.image},link:{_text:t.link}}),i.rss.channel.category=Array.from(e.categories).map(r=>({_text:r})),i.rss.channel.item=e.items.map(r=>{const n={title:{_text:r.title},link:{_text:r.link},guid:_t(r),source:{_attributes:{url:a.rss},_text:r.title}};r.description&&(n.description={_text:r.description});const o=r.author.find(l=>l.email&&l.name);return o&&(n.author={_text:`${o.email} (${o.name})`}),r.category&&(n.category=r.category.filter(l=>l.name).map(l=>bt(l))),r.pubDate&&(n.pubDate={_text:r.pubDate.toUTCString()}),r.content&&(s=!0,n["content:encoded"]={_cdata:r.content}),r.enclosure&&(n.enclosure=xt(r.enclosure)),n}),s&&(i.rss._attributes["xmlns:content"]="http://purl.org/rss/1.0/modules/content/",i.rss._attributes["xmlns:dc"]="http://purl.org/dc/elements/1.1/"),B(T(i),{compact:!0,ignoreComment:!0,spaces:2})},G=(e,t,a)=>{const s=w(b(t).map(([i,r])=>[i,new gt(e,r,i)]));return b(t).filter(([,{atom:i,json:r,rss:n}])=>i||r||n).map(([i,r])=>{const{atom:n,json:o,rss:l,count:p=100,filter:y,sorter:d}=r,m=s[i],h=e.pages.filter(X=>X.pathLocale===i).filter(y).sort(d);for(const X of h){const Y=new mt(e,r,X,a);if(m.add(Y),m.items.length===p)break}const g=m.items.length;A.succeed(`added ${I.cyan(`${g} page${g>1?"s":""}`)} as feed item${g>1?"s":""} in locale ${I.cyan(i)}`);const{atomOutputFilename:F,jsonOutputFilename:S,rssOutputFilename:U}=O(r,i),$=[];return n&&$.push([F,ft(m),"application/atom+xml"]),o&&$.push([S,yt(m),"application/json"]),l&&$.push([U,Ot(m),"application/xml"]),$}).flat()},Ft=({siteData:e},t)=>w(J({"/":{},...e.locales}).map(a=>{const s=t.locales?.[a]?.preservedElements??t.preservedElements,{hostname:i,devServer:r,locales:n,...o}=t;return[a,{filter:({frontmatter:l,filePathRelative:p})=>!!(l.feed??(p&&!l.home)),sorter:(l,p)=>et(l.data.git?.createdTime?new Date(l.data.git.createdTime):l.frontmatter.date,p.data.git?.createdTime?new Date(p.data.git.createdTime):p.frontmatter.date),...o,...t.locales?.[a],hostname:i,isPreservedElement:f(s)?l=>s.some(p=>p instanceof RegExp?p.test(l):p===l):u(s)?s:()=>!1}]})),St=import.meta.dirname||nt(import.meta.url),V=rt(q.resolve(St,"../../templates")),vt=C.readFileSync(`${V}atom.xsl`,"utf-8"),kt=C.readFileSync(`${V}rss.xsl`,"utf-8"),W=e=>b(e).filter(([,{atom:t}])=>t).map(([t,a])=>{const{atomXslTemplate:s=vt}=a,{atomXslFilename:i}=O(a,t);return[i,s]}),Q=e=>b(e).filter(([,{rss:t}])=>t).map(([t,a])=>{const{rssXslTemplate:s=kt}=a,{rssXslFilename:i}=O(a,t);return[i,s]}),N=(e,t)=>t.map(async([a,s])=>{const i=e.dir.dest(a);await C.ensureDir(q.dirname(i)),await C.writeFile(i,s,"utf-8")}),Dt=e=>t=>{t.env.isDebug&&A.info("Options:",e);const a={name:E};let s=t.env.isDev?e.devHostname||`http://localhost:${t.options.port}`:e.hostname;if(!s)return A.error(`Option ${I.magenta("hostname")} is required!`),a;if(s=L(_(s)?s:`https://${s}`),!e.atom&&!e.json&&!e.rss&&e.locales&&it(e.locales).every(({atom:r,json:n,rss:o})=>!r&&!n&&!o))return A.info("No feed output requested, the plugin won’t start!"),a;const i=Ft(t,e);return{...a,onInitialized:()=>{(t.env.isBuild||e.devServer)&&ct(t,i)},extendsBundlerOptions:r=>{e.devServer&&[...G(t,i,s),...W(i),...Q(i)].forEach(([n,o,l])=>{st(r,t,{path:n,response:(p,y)=>(l&&y.setHeader("Content-Type",l),Promise.resolve(o)),errMsg:"Unexpected feed generation error"})})},onGenerated:async()=>{await Promise.all([...N(t,G(t,i,s)),...N(t,W(i)),...N(t,Q(i))])}}};export{Dt as feedPlugin}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@vuepress/plugin-feed", | ||
"version": "2.0.0-rc.66", | ||
"version": "2.0.0-rc.68", | ||
"description": "Feed plugin for vuepress-theme-hope", | ||
@@ -39,3 +39,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@vuepress/helper": "2.0.0-rc.66", | ||
"@vuepress/helper": "2.0.0-rc.68", | ||
"xml-js": "^1.6.11" | ||
@@ -49,3 +49,3 @@ }, | ||
}, | ||
"gitHead": "adcdae0596640a5516c89235fe1b99ea821ab4b0" | ||
"gitHead": "c8388100e54251c026b8039fd25f62c807bd3d23" | ||
} |
Sorry, the diff of this file is not supported yet
126269
48
+ Added@types/node@22.12.0(transitive)
+ Added@vuepress/helper@2.0.0-rc.68(transitive)
+ Addedfastq@1.18.0(transitive)
+ Addedora@8.1.1(transitive)
- Removed@types/node@22.13.0(transitive)
- Removed@vuepress/helper@2.0.0-rc.66(transitive)
- Removedfastq@1.19.0(transitive)
- Removedora@8.2.0(transitive)
Updated@vuepress/helper@2.0.0-rc.68