feat: rss limit (closes #459)
This commit is contained in:
		
							parent
							
								
									a19df64be8
								
							
						
					
					
						commit
						71d81bde1d
					
				
					 1 changed files with 9 additions and 3 deletions
				
			
		| 
						 | 
					@ -18,12 +18,14 @@ export type ContentDetails = {
 | 
				
			||||||
interface Options {
 | 
					interface Options {
 | 
				
			||||||
  enableSiteMap: boolean
 | 
					  enableSiteMap: boolean
 | 
				
			||||||
  enableRSS: boolean
 | 
					  enableRSS: boolean
 | 
				
			||||||
 | 
					  rssLimit?: number
 | 
				
			||||||
  includeEmptyFiles: boolean
 | 
					  includeEmptyFiles: boolean
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const defaultOptions: Options = {
 | 
					const defaultOptions: Options = {
 | 
				
			||||||
  enableSiteMap: true,
 | 
					  enableSiteMap: true,
 | 
				
			||||||
  enableRSS: true,
 | 
					  enableRSS: true,
 | 
				
			||||||
 | 
					  rssLimit: 10,
 | 
				
			||||||
  includeEmptyFiles: true,
 | 
					  includeEmptyFiles: true,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -39,7 +41,7 @@ function generateSiteMap(cfg: GlobalConfiguration, idx: ContentIndex): string {
 | 
				
			||||||
  return `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">${urls}</urlset>`
 | 
					  return `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">${urls}</urlset>`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function generateRSSFeed(cfg: GlobalConfiguration, idx: ContentIndex): string {
 | 
					function generateRSSFeed(cfg: GlobalConfiguration, idx: ContentIndex, limit?: number): string {
 | 
				
			||||||
  const base = cfg.baseUrl ?? ""
 | 
					  const base = cfg.baseUrl ?? ""
 | 
				
			||||||
  const root = `https://${base}`
 | 
					  const root = `https://${base}`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -53,13 +55,17 @@ function generateRSSFeed(cfg: GlobalConfiguration, idx: ContentIndex): string {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const items = Array.from(idx)
 | 
					  const items = Array.from(idx)
 | 
				
			||||||
    .map(([slug, content]) => createURLEntry(simplifySlug(slug), content))
 | 
					    .map(([slug, content]) => createURLEntry(simplifySlug(slug), content))
 | 
				
			||||||
 | 
					    .slice(0, limit ?? idx.size)
 | 
				
			||||||
    .join("")
 | 
					    .join("")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return `<?xml version="1.0" encoding="UTF-8" ?>
 | 
					  return `<?xml version="1.0" encoding="UTF-8" ?>
 | 
				
			||||||
<rss version="2.0">
 | 
					<rss version="2.0">
 | 
				
			||||||
    <channel>
 | 
					    <channel>
 | 
				
			||||||
      <title>${escapeHTML(cfg.pageTitle)}</title>
 | 
					      <title>${escapeHTML(cfg.pageTitle)}</title>
 | 
				
			||||||
      <link>${root}</link>
 | 
					      <link>${root}</link>
 | 
				
			||||||
      <description>Recent content on ${cfg.pageTitle}</description>
 | 
					      <description>${!!limit ? `Last ${limit} notes` : "Recent notes"} on ${
 | 
				
			||||||
 | 
					        cfg.pageTitle
 | 
				
			||||||
 | 
					      }</description>
 | 
				
			||||||
      <generator>Quartz -- quartz.jzhao.xyz</generator>
 | 
					      <generator>Quartz -- quartz.jzhao.xyz</generator>
 | 
				
			||||||
      ${items}
 | 
					      ${items}
 | 
				
			||||||
    </channel>
 | 
					    </channel>
 | 
				
			||||||
| 
						 | 
					@ -102,7 +108,7 @@ export const ContentIndex: QuartzEmitterPlugin<Partial<Options>> = (opts) => {
 | 
				
			||||||
      if (opts?.enableRSS) {
 | 
					      if (opts?.enableRSS) {
 | 
				
			||||||
        emitted.push(
 | 
					        emitted.push(
 | 
				
			||||||
          await emit({
 | 
					          await emit({
 | 
				
			||||||
            content: generateRSSFeed(cfg, linkIndex),
 | 
					            content: generateRSSFeed(cfg, linkIndex, opts.rssLimit),
 | 
				
			||||||
            slug: "index" as FullSlug,
 | 
					            slug: "index" as FullSlug,
 | 
				
			||||||
            ext: ".xml",
 | 
					            ext: ".xml",
 | 
				
			||||||
          }),
 | 
					          }),
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue