@stefanprobst/remark-excerpt
TypeScript icon, indicating that this package has built-in type declarations

2.0.2 • Public • Published

remark-excerpt

remark transformer plugin to extract an excerpt.

How to install

yarn add @stefanprobst/remark-excerpt

How to use

The plugin creates an excerpt, which includes content until a top-level <!-- excerpt --> comment. If no excerpt comment is found content will be truncated at maxLength.

Examples:

import { remark } from 'remark'
import excerpt from '@stefanprobst/remark-excerpt'

const contentWithComment = `
This is some text.

<!-- excerpt -->

This is some more text.
`

const contentWithoutComment = `
This is some text. This is some more text.
`

const processor = remark().use(excerpt, { maxLength: 25 })

console.log(processor.processSync(contentWithComment))
// This is some text.
console.log(processor.processSync(contentWithoutComment))
// This is some text. This...

Options

  • maxLength: truncate text at this length if no <!-- excerpt --> comment is found. defaults to 140 characters.
  • ellipsis: if text is truncated, add this ellipsis. defaults to "...".
  • preferWordBoundaries: truncate at word boundary. defaults to false.

Usage in a processor pipeline

It is possible to use the plugin in "bridge mode" in a unified processor pipeline without truncating the original tree:

import { remark } from 'remark'
import excerpt from '@stefanprobst/remark-excerpt'
import bridge from '@stefanprobst/unified-util-bridge'

const content = `
This is some text. This is some more text.
`

const excerptProcessor = remark().use(excerpt, { maxLength: 25 })
const processor = remark().use(bridge, 'excerpt', excerptProcessor)

const { data, contents } = processor.processSync(content)

console.log(contents)
// This is some text. This is some more text.
console.log(data.excerpt)
// This is some text. This...

Readme

Keywords

none

Package Sidebar

Install

npm i @stefanprobst/remark-excerpt

Weekly Downloads

19

Version

2.0.2

License

MIT

Unpacked Size

7.45 kB

Total Files

5

Last publish

Collaborators

  • stefanprobst