包详细信息

@gerhobbelt/markdown-it-title

GerHobbelt20Unlicense4.0.0-8

Extract title during markdown-it parsing.

markdown-it-plugin, markdown-it, markdown

自述文件

markdown-it-title npm version

Extract title during markdown-it parsing.

Overview

This plugin is useful if you want to write plain markdown files, e.g. without YAML front matter, and want to extract the document title to populate a HTML <title> tag, Open Graph and so on.

Additionally, markdown-it-title supports extracting the contents of the first paragraphs if you want to use them as meta description, post excerpt and so forth.

Usage

const opts = {
  level: 1,
  excerpt: 2
}
const md = require('markdown-it')()
  .use(require('markdown-it-title'), opts)

// Find whatever heading comes first.
// md.use(require('markdown-it-title'), { level: 0 })

// Find `<h2>`.
// md.use(require('markdown-it-title'), { level: 2 })

const env = {}

md.render(`# [Hello](#hello), *world*!

This is the **first** paragraph.

## Hi, I'm a title

Another one.

And another one.
`, env)

env.title === 'Hello, world!'
env.excerpt[0] === 'This is the first paragraph.'
env.excerpt[1] === 'Another one.'

The opts object can contain:

Name Description Default
level Heading level to look for the title. Use 0 to take whichever heading comes first. 1
excerpt Number of paragraphs to extract from the beginning of the document to be used as page excerpt. 0

Setting level to 1 (the default) will extract the contents of the first <h1> to env.title. If set to 0, markdown-it-title will take the first heading it encounters.

Also, the first excerpt paragraphs will be extracted in the env.excerpt array (not enabled by default).

Both title and excerpt text won't include any markup.

更新日志

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

Unreleased

[4.0.0] - 2021-06-15

  • Add support for extracting excerpt text from leading paragraphs.

3.0.0 - 2020-03-29

  • Fix inline code support.
  • ES6 by default, remove build step.

2.0.0 - 2020-01-16

  • Exclude tags from title extraction.

1.0.4 - 2015-08-13

  • Allow running more than once. (#1)

1.0.3 - 2015-05-25

  • Fix destructuring syntax.

1.0.2 - 2015-03-17

  • Refactor using markdown-it render rules.

1.0.1 - 2015-03-17

  • Fix ES6 support issue.

1.0.0 - 2015-03-17

  • Initial release.