透過 100 多個技巧的集合學習 Nuxt!

nuxt-feedme

適用於 Nuxt Web 框架的 RSS feed 模組,支援 ATOM、JSON 和 RSS

nuxt-feedme

npm version npm downloads License Nuxt

此模組為 RSS feed 的實作提供額外的功能。它與 module-feed 非常相似,但支援 nuxt-content

如果您需要完全客製化的 feed,您可以自由選擇任何 feed 模組(此模組或上述提到的模組)。但此模組可能更具彈性。

特色

  • nuxt-content 開箱即用配置
  • 支援兩種 feed 類型的通用和專用鉤子
  • 彈性:使用配置預設值(feed、item)、映射 (item) 或鉤子進行客製化
  • 支援 SSR 和 SSG

nuxt-content 開箱即用配置

預設設定為

{
  feeds: {
    '/feed.atom': { revisit: '6h', type: 'atom1', content: true },
    '/feed.xml': { revisit: '6h', type: 'rss2', content: true },
    '/feed.json': { revisit: '6h', type: 'json1', content: true },
  },
  content: {
    item: {
      templateRoots: [true, 'feedme'],
    },
  },
}

通用和專用鉤子

// project-name/server/plugins/feedme.ts
import type { NitroApp } from 'nitropack'

// Nitro hooks can be set only in nitro plugin
export default (nitroApp: NitroApp) => {
  // General hook: feedme:handle:content:item
  // Specialized hook: feedme:handle:content:item[*]
  //
  // When specialized hook set, general also will be called
  nitroApp.hooks.hook('feedme:handle:content:item[/contentDefaults.xml]', async ({ feed: { insert, invoke, parsed } }) => {
    if (parsed.title === 'First item') {
      // Invoke in case if item was created by another callback
      const maybeItemOptions = invoke()

      // Insert replaces current item configuration
      insert({
        ...maybeItemOptions,
        category: [
          ...maybeItemOptions?.category ?? [],
          { name: 'content hook processed' },
        ],
      })
    }
  })

  // Specialized hook for default feed
  nitroApp.hooks.hook('feedme:handle[/feed.xml]', async ({ context: { event }, feed: { create } }) => {
    // Create also replaces current feed
    create({ id: '', title: `Special feed for '${event.path}' route`, copyright: '' })
  })

  // General hook for default feed
  nitroApp.hooks.hook('feedme:handle', async ({ context: { event }, feed: { create, invoke } }) => {
    invoke() ?? create({ id: '', title: `Default feed for '${event.path}' route`, copyright: '' })
  })
}

映射配置

映射用於將 feed item 物件鍵連結到已解析內容中的路徑

{
  item: {
    mapping: [
      // Third item is optional mapping function
      ['date', 'modified', value => value ? new Date(value) : value],
      // When mapping function result is undefined - next variant applied
      ['date', 'created', value => value ? new Date(value) : value],
      // Until the real one value will be set
      ['date', '', () => new Date()],
      // By default mapping is x => x
      ['link', '_path'],
    ],
    // Create default mappings with indicated roots:
    //   [keyof Item /* such as image, id, link */, root + keyof Item]
    //
    // The true value means use empty root:
    //   ['link', 'link']
    //
    // Where any other key means use this as path to value:
    //  ['link', `{root}.link`]
    //
    // Root can be separate by `.` which allows to deep invoking
    templateRoots: [true, 'feedme'],
  }
}

注意:日期值是 feed 模組的特殊情況,因此預設情況下,映射為日期欄位提供以下映射:value => value ? new Date(value) : new Date() 因此,如果您為日期提供自己的別名,則需要提供映射函式

注意:映射函式是序列化的,因此必須避免在外部範圍中包含任何引用

標籤

標籤允許根據匹配項替換節點值

{
  // Allows to pass optional map function
  tags: [
    // This tags replace first empty symbol if value starts with /
    // Example: /link -> urlBase/link
    [/^(?=\/)/, urlBase],
  ],
}

注意:標籤會遞迴應用,item.field.inner (value) 會受到影響

快速設定

  1. nuxt-feedme 相依性新增至您的專案

使用您最喜歡的套件管理器(我偏好 yarn)

yarn add -D nuxt-feedme

pnpm add -D nuxt-feedme

npm install --save-dev nuxt-feedme
  1. nuxt-feedme 新增至 nuxt.config.tsmodules 區段
export default defineNuxtConfig({
  modules: [
    // After nuxt content
    '@nuxt/content',
    'nuxt-feedme'
  ]
})

就這樣!您現在可以在您的 Nuxt 應用程式中使用 nuxt-feedme