2.0.2 • Published 2 years ago

@stefanprobst/remark-excerpt v2.0.2

Weekly downloads
27
License
MIT
Repository
github
Last release
2 years ago

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...
2.0.2

2 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago