marked-gfm-heading-id
Advanced tools
Comparing version 3.0.8 to 3.1.0
@@ -89,2 +89,4 @@ (function (global, factory) { | ||
let headings = []; | ||
function gfmHeadingId({ prefix = '' } = {}) { | ||
@@ -95,2 +97,3 @@ return { | ||
preprocess(src) { | ||
headings = []; | ||
slugger = new BananaSlug(); | ||
@@ -102,4 +105,11 @@ return src; | ||
heading(text, level, raw) { | ||
raw = raw.toLowerCase().trim().replace(/<[!\/a-z].*?>/ig, ''); | ||
return `<h${level} id="${prefix}${slugger.slug(raw)}">${text}</h${level}>\n`; | ||
raw = raw | ||
.toLowerCase() | ||
.trim() | ||
.replace(/<[!\/a-z].*?>/gi, ''); | ||
const id = `${prefix}${slugger.slug(raw)}`; | ||
const heading = { level, text, id }; | ||
headings.push(heading); | ||
return `<h${level} id="${id}">${text}</h${level}>\n`; | ||
} | ||
@@ -110,4 +120,9 @@ } | ||
function getHeadingList() { | ||
return headings; | ||
} | ||
exports.getHeadingList = getHeadingList; | ||
exports.gfmHeadingId = gfmHeadingId; | ||
})); |
{ | ||
"name": "marked-gfm-heading-id", | ||
"version": "3.0.8", | ||
"version": "3.1.0", | ||
"description": "marked GFM heading ids", | ||
@@ -48,4 +48,4 @@ "main": "./lib/index.cjs", | ||
"devDependencies": { | ||
"@babel/core": "^7.22.15", | ||
"@babel/preset-env": "^7.22.15", | ||
"@babel/core": "^7.22.20", | ||
"@babel/preset-env": "^7.22.20", | ||
"@rollup/plugin-node-resolve": "^15.2.1", | ||
@@ -55,14 +55,14 @@ "@semantic-release/changelog": "^6.0.3", | ||
"@semantic-release/git": "^10.0.1", | ||
"@semantic-release/github": "^9.0.4", | ||
"@semantic-release/npm": "^10.0.5", | ||
"@semantic-release/release-notes-generator": "^11.0.7", | ||
"babel-jest": "^29.6.4", | ||
"eslint": "^8.48.0", | ||
"@semantic-release/github": "^9.0.6", | ||
"@semantic-release/npm": "^11.0.0", | ||
"@semantic-release/release-notes-generator": "^12.0.0", | ||
"babel-jest": "^29.7.0", | ||
"eslint": "^8.49.0", | ||
"eslint-config-standard": "^17.1.0", | ||
"eslint-plugin-import": "^2.28.1", | ||
"eslint-plugin-n": "^16.0.2", | ||
"eslint-plugin-n": "^16.1.0", | ||
"eslint-plugin-promise": "^6.1.1", | ||
"jest-cli": "^29.6.4", | ||
"marked": "^9.0.0", | ||
"rollup": "^3.28.1", | ||
"jest-cli": "^29.7.0", | ||
"marked": "^9.0.3", | ||
"rollup": "^3.29.2", | ||
"semantic-release": "^21.1.1", | ||
@@ -69,0 +69,0 @@ "tsd": "^0.29.0" |
@@ -25,2 +25,36 @@ # marked-gfm-heading-id | ||
## Get heading list | ||
`getHeadingList` is a function that is exported to provide the list of headings. | ||
The headings will each be an object with the following properties: | ||
- `text`: The rendered HTML for the heading | ||
- `level`: The heading level (1-7) | ||
- `id`: The id given to the heading including any prefix | ||
```js | ||
import { marked } from "marked"; | ||
import { gfmHeadingId, getHeadingList } from "marked-gfm-heading-id"; | ||
marked.use(gfmHeadingId({prefix: "my-prefix-"}), { | ||
hooks: { | ||
postprocess(html) { | ||
const headings = getHeadingList(); | ||
return ` | ||
<ul id="table-of-contents"> | ||
${headings.map(({id, text, level}) => `<li><a href="#${id}" class="h${level}">${text}</a></li>`)} | ||
</ul> | ||
${html}`; | ||
} | ||
} | ||
}); | ||
marked("# heading"); | ||
// <ul id="table-of-contents"> | ||
// <li><a href="#my-prefix-heading" class="h1">heading</a></li> | ||
// </ul> | ||
// <h1 id="my-prefix-heading">heading</h1> | ||
``` | ||
## `options` | ||
@@ -27,0 +61,0 @@ |
@@ -1,2 +0,2 @@ | ||
import type { MarkedExtension, marked } from "marked" | ||
import type {MarkedExtension, marked} from 'marked'; | ||
@@ -13,4 +13,23 @@ /** Options for configuring marked-gfm-heading-id extension */ | ||
* @param options Options for the extension | ||
* @returns A {@link marked.MarkedExtension | MarkedExtension} to be passed to {@link marked.use | `marked.use()`} | ||
* @returns A {@link marked.MarkedExtension | MarkedExtension} to be passed | ||
* to {@link marked.use | `marked.use()`} | ||
*/ | ||
export function gfmHeadingId(options?: GfmHeadingIdOptions): MarkedExtension; | ||
/** | ||
* Headings information, can be used to create table of content | ||
*/ | ||
export interface HeadingData { | ||
level: number; | ||
text: string; | ||
id: string; | ||
} | ||
/** | ||
* Returns a list of headings with the ids as computed by gfmHeadingId | ||
* | ||
* @param tokens a lexer output | ||
* @param options Options for the extension | ||
* @returns A string formatted the same as what would {@link gfmHeadingId} do. | ||
*/ | ||
export function getHeadingList(): HeadingData[]; |
import GithubSlugger from 'github-slugger'; | ||
let slugger; | ||
let headings = []; | ||
export function gfmHeadingId({ prefix = '' } = {}) { | ||
@@ -9,2 +11,3 @@ return { | ||
preprocess(src) { | ||
headings = []; | ||
slugger = new GithubSlugger(); | ||
@@ -16,4 +19,11 @@ return src; | ||
heading(text, level, raw) { | ||
raw = raw.toLowerCase().trim().replace(/<[!\/a-z].*?>/ig, ''); | ||
return `<h${level} id="${prefix}${slugger.slug(raw)}">${text}</h${level}>\n`; | ||
raw = raw | ||
.toLowerCase() | ||
.trim() | ||
.replace(/<[!\/a-z].*?>/gi, ''); | ||
const id = `${prefix}${slugger.slug(raw)}`; | ||
const heading = { level, text, id }; | ||
headings.push(heading); | ||
return `<h${level} id="${id}">${text}</h${level}>\n`; | ||
} | ||
@@ -23,1 +33,5 @@ } | ||
} | ||
export function getHeadingList() { | ||
return headings; | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28789
331
64
6