@canonical/latest-news

中文文档
2.3.1发布于 2026/08/31ISCCommonJS零依赖近 30 天下载 1.6万

A script that loads blogs posts into a given template

原始使用文档
文档目录7 节
  1. Usage
  2. Templates
  3. Options
  4. Building
  5. Watching the changed files
  6. Run the tests
  7. Release process

Canonical Latest News

This project contains the JavaScript to display the latest blog posts of a given topic.

Usage

Use a node package manager to install this component and then link the JS file beneath the template, with settings.

  1. Install via yarn or npm
yarn add @canonical/latest-news

...or...

npm install @canonical/latest-news --save
  1. You can then install the library either by directly linking to it or via ES6 imports.

To consume the library directly, add a link to the JS file containing an IIFE and run the `canonicalLatestNews.fetchLatestNews() function:

<script src="/node_modules/@canonical/latest-news/dist/iife.js"></script>
<script>
  canonicalLatestNews.fetchLatestNews({
    /* options */
  });
</script>

To import it, simply call it from your site-wide JS file:

import { fetchLatestNews } from "@canonical/latest-news";
fetchLatestNews({
  /* options */
});

Templates

You will need a template that follows this structure to display the latest news feed:

<div id="latest-news-container">
  Loading...
</div>

<template style="display:none" id="articles-template">
  <div class="article-image"></div>
  <h4><a class="article-link article-title"></a></h4>
  <p>
    <em><time datetime="" class="article-time"></time></em>
  </p>
  <p class="article-author"></p>
</template>

The script will look for the following class names to use as hooks for content within the template:

  • article-time: The time the article was published formatted as 2 May 2020
  • article-link: The permalink for the article
  • article-title: The title of the article
  • article-image: The featured image of the article
  • article-excerpt: The excerpt of the article
  • article-group: The group of the article
  • article-author: The author(s) of the article

You can choose what content to display and how it will look by using the above classes. If you don't want a certain part of the content, for example the article image, then do not include an element with the class name of article-image.

Blog Pattern

You can also render blog posts fetched by this module using Jinja pattern called Blog. More information about the pattern and its usage can be found in the Vanilla documentation.

Options

You will need to pass some options to the script in order for it to know where the template is and where it should be rendered to. These are:

  • articlesContainerSelector: String - The container where the articles will be displayed
  • articleTemplateSelector: String - The template that will be used for the article
  • allArticles: Boolean - Fetch the latest posts in a single list regardless of sticky status, so featured (sticky) posts are included in the main container instead of being split into the spotlight slot (Optional)
  • groupId: Integer - Return posts in a specific group (Optional)
  • gtmEventLabel: String - An event label used for Google Analytics (Optional)
  • hostname: String - An optional hostname to be used for the permalink. By default the link is relative (Optional)
  • imageClasses: Array - A list of classes that will be added to the image element (Optional)
  • limit: Integer - The number of posts to be returned (Optional)
  • linkImage: Boolean - Wrap the thumbnail image in a link (Optional)
  • spotlightContainerSelector: String - The container where the spotlight article will be displayed (Optional)
  • spotlightTemplateSelector: String - The template that will be used for the spotlight article (Optional)
  • tagId: Integer - Return posts with a specific tag (Optional)
  • tagIds: String(comma separated tagids i.e. "id1,id2,id3") - Return posts with all tags (Optional)
  • excerptLength: Integer - Specifies the approximate number of characters included in the excerpt (Optional)
  • lazyLoadImage: Boolean - Enable lazy loading for images to improve page performance by loading images only when they enter the viewport. By default, it is true (Optional)
  • url: String - By default API URL is /blog/latest-news, but in case it should be something else you can pass this optional parameter (Optional)

Building

To build the JS into the /dist folder, run:

yarn build

Watching the changed files

yarn watch

Run the tests

yarn test

Release process

The package is versioned using semantic versioning and published to the NPM registry.

To cut a new release run:

npm version [patch|minor|major]

This will trigger the prepublishonly script which will ensure requisite artefacts are built before publishing.

Code licensed LGPLv3 by Canonical Ltd.

With ♥ from Canonical

版本与发布记录

@canonical/latest-news 共发布 18 个版本,首次发布于 2020/05/18;过去 12 个月发布了 4 个版本。

版本发布日期距今
2.3.1latest2026/08/312 个月前
2.2.12025/12/1210 个月前
2.2.02025/11/2811 个月前
2.1.12025/11/2611 个月前
2.1.02025/10/241 年前
2.0.02025/08/281.2 年前

关于 @canonical/latest-news 的常见问题

@canonical/latest-news 是什么?

A script that loads blogs posts into a given template

如何安装 @canonical/latest-news?

在项目目录运行 npm install @canonical/latest-news,或使用 pnpm add / yarn add / bun add。

@canonical/latest-news 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/canonical__latest-news 是否存在。

@canonical/latest-news 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@canonical/latest-news 可以商用吗?

@canonical/latest-news 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@canonical/latest-news 的下载量有多少?

根据 npm 官方下载统计,@canonical/latest-news 在 2026/10/09 – 2026/11/07 这 30 天内被下载 15,781 次,日均约 526 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@canonical/latest-news 还在维护吗?

最新版本 2.3.1 发布于 2026/08/31(2 个月前);至今共发布 18 个版本,其中过去 12 个月发布了 4 个;首次发布于 2020/05/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。