Skip to content

Commit

Permalink
fix: reset automatically before marked runs
Browse files Browse the repository at this point in the history
BREAKING CHANGE: remove reset export
  • Loading branch information
UziTech committed Apr 5, 2023
1 parent 4ce418e commit 35771aa
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 92 deletions.
21 changes: 3 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ Add ids to headings like GitHub.
import { marked } from "marked";
import { gfmHeadingId } from "marked-gfm-heading-id";

// or ES Module script
// import { marked } from "https://cdn.jsdelivr.net/gh/markedjs/marked/src/marked.js";
// import { gfmHeadingId } from "https://cdn.jsdelivr.net/gh/UziTech/marked-gfm-heading-id/src/index.js";
// or UMD script
// <script src="https://cdn.jsdelivr.net/npm/marked/lib/marked.umd.js"></script>
// <script src="https://cdn.jsdelivr.net/npm/marked-gfm-heading-id/lib/index.umd.js"></script>

const options = {
prefix: "my-prefix-",
Expand All @@ -22,21 +22,6 @@ marked("# heading");
// <h1 id="my-prefix-heading">heading</h1>
```

## `reset`

If you want to reset the heading list between `marked` calls you must call `reset`.

```js
import { marked } from "marked";
import { gfmHeadingId, reset } from "marked-gfm-heading-id";

marked.use(gfmHeadingId());

marked("# heading\n\n# heading");
reset();
marked("# heading\n\n# heading");
```

## `options`

| option | type | default | description |
Expand Down
13 changes: 6 additions & 7 deletions lib/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ var GithubSlugger = require('github-slugger');

let slugger;

function reset() {
slugger = new GithubSlugger();
}

function gfmHeadingId({ prefix = '' } = {}) {
reset();

return {
hooks: {
preprocess(src) {
slugger = new GithubSlugger();
return src;
}
},
renderer: {
heading(text, level, raw) {
raw = raw.toLowerCase().trim().replace(/<[!\/a-z].*?>/ig, '');
Expand All @@ -22,4 +22,3 @@ function gfmHeadingId({ prefix = '' } = {}) {
}

exports.gfmHeadingId = gfmHeadingId;
exports.reset = reset;
15 changes: 7 additions & 8 deletions lib/index.umd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.markedBidi = {}));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.markedGfmHeadingId = {}));
})(this, (function (exports) { 'use strict';

// This module is generated by `script/`.
Expand Down Expand Up @@ -86,14 +86,14 @@

let slugger;

function reset() {
slugger = new BananaSlug();
}

function gfmHeadingId({ prefix = '' } = {}) {
reset();

return {
hooks: {
preprocess(src) {
slugger = new BananaSlug();
return src;
}
},
renderer: {
heading(text, level, raw) {
raw = raw.toLowerCase().trim().replace(/<[!\/a-z].*?>/ig, '');
Expand All @@ -104,6 +104,5 @@
}

exports.gfmHeadingId = gfmHeadingId;
exports.reset = reset;

}));
42 changes: 21 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@
},
"homepage": "https://github.com/markedjs/marked-gfm-heading-id#readme",
"peerDependencies": {
"marked": ">1 <5"
"marked": "^4.3.0"
},
"dependencies": {
"github-slugger": "^2.0.0"
},
"devDependencies": {
"@babel/core": "^7.21.4",
"@babel/preset-env": "^7.21.4",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-node-resolve": "^15.0.2",
"@semantic-release/changelog": "^6.0.3",
"@semantic-release/commit-analyzer": "^9.0.2",
"@semantic-release/git": "^10.0.1",
Expand Down
63 changes: 33 additions & 30 deletions spec/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { marked } from 'marked';
import { gfmHeadingId, reset } from '../src/index.js';
import { gfmHeadingId } from '../src/index.js';

describe('marked-gfm-heading-id', () => {
beforeEach(() => {
Expand All @@ -12,13 +12,13 @@ describe('marked-gfm-heading-id', () => {
# heading
# heading
`.trimStart();
`;

const html = `
<h1 id="heading">heading</h1>
expect(marked(markdown)).toMatchInlineSnapshot(`
"<h1 id="heading">heading</h1>
<h1 id="heading-1">heading</h1>
`.trimStart();
expect(marked(markdown)).toBe(html);
"
`);
});

test('prefix', () => {
Expand All @@ -27,30 +27,33 @@ describe('marked-gfm-heading-id', () => {
# heading
# heading
`.trimStart();
`;

const html = `
<h1 id="my-prefix-heading">heading</h1>
expect(marked(markdown)).toMatchInlineSnapshot(`
"<h1 id="my-prefix-heading">heading</h1>
<h1 id="my-prefix-heading-1">heading</h1>
`.trimStart();
expect(marked(markdown)).toBe(html);
"
`);
});

test('reset', () => {
test('reset for new marked call', () => {
marked.use(gfmHeadingId());
const markdown = `
# heading
# heading
`.trimStart();
`;

const html = `
<h1 id="heading">heading</h1>
expect(marked(markdown)).toMatchInlineSnapshot(`
"<h1 id="heading">heading</h1>
<h1 id="heading-1">heading</h1>
`.trimStart();
expect(marked(markdown)).toBe(html);
reset();
expect(marked(markdown)).toBe(html);
"
`);
expect(marked(markdown)).toMatchInlineSnapshot(`
"<h1 id="heading">heading</h1>
<h1 id="heading-1">heading</h1>
"
`);
});

test('weird headings', () => {
Expand All @@ -65,17 +68,17 @@ describe('marked-gfm-heading-id', () => {
# Html in <em>header</em>
# just <tags>test
# just <tags>test 2</tags>
# just <tags> test 2 spaces </tags>
# just <tags>test 3</any>
# just <tags>test 4<any>
# just <non tags
# just <tags with>spaces
# just <#$%> weird chars
Expand All @@ -91,10 +94,10 @@ describe('marked-gfm-heading-id', () => {
# Hello **world!**
# <samp>Hello <ins>world!</ins></samp>
`.trimStart();
`;

const html = `
<h1 id="foo-1">foo 1</h1>
expect(marked(markdown)).toMatchInlineSnapshot(`
"<h1 id="foo-1">foo 1</h1>
<h1 id="foo">foo</h1>
<h1 id="foo-2">foo</h1>
<h1 id="html-in-header">Html in <em>header</em></h1>
Expand All @@ -112,7 +115,7 @@ describe('marked-gfm-heading-id', () => {
<h1 id="comment-">comment <!-- inside --></h1>
<h1 id="hello-world">Hello <strong>world!</strong></h1>
<h1 id="hello-world-1"><samp>Hello <ins>world!</ins></samp></h1>
`.trimStart();
expect(marked(markdown)).toBe(html);
"
`);
});
});
12 changes: 6 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import GithubSlugger from 'github-slugger';
let slugger;

export function reset() {
slugger = new GithubSlugger();
}

export function gfmHeadingId({ prefix = '' } = {}) {
reset();

return {
hooks: {
preprocess(src) {
slugger = new GithubSlugger();
return src;
}
},
renderer: {
heading(text, level, raw) {
raw = raw.toLowerCase().trim().replace(/<[!\/a-z].*?>/ig, '');
Expand Down

0 comments on commit 35771aa

Please sign in to comment.