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

nuxt-feedme

適用於 Nuxt web framework 且支援 ATOM、JSON 和 RSS 的 RSS feed 模組

nuxt-feedme

npm versionnpm downloadsLicenseNuxt

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

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

功能

  • 針對 nuxt-content 開箱即用進行配置
  • 支援通用和專用 hook,適用於兩種 feed 類型
  • 彈性:使用預設配置(feed、item)、映射(item)或 hook 進行客製化
  • 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'],
    },
  },
}

通用和專用 hook

// 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 物件的 key 連結到已解析內容中的路徑

{
  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'],
  }
}

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

注意:映射函數已序列化,因此需要不包含外部範圍內的任何參考

標籤

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

{
  // 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

或透過 nuxi module 安裝

npx nuxi@latest module add nuxt-feedme
  1. nuxt-feedme 新增至 nuxt.config.tsmodules 區段
export default defineNuxtConfig({
  modules: [
    // After nuxt content
    '@nuxt/content',
    'nuxt-feedme'
  ]
})

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