mirror of
https://github.com/markedjs/marked
synced 2024-11-22 17:37:24 +00:00
use options param, update docs, update tests
This commit is contained in:
parent
2290f677c9
commit
710658cb61
@ -82,12 +82,12 @@ slugger.slug('foo-1') // foo-1-2
|
||||
...
|
||||
```
|
||||
|
||||
`slugger` also has a stateless method `slugText` which does not update the internal accumulator:
|
||||
`slugger.slug` can also be called with the `dryrun` option for stateless operation:
|
||||
```js
|
||||
slugger.slug('foo') // foo
|
||||
slugger.slugText('foo') // foo-1
|
||||
slugger.slug('foo', { dryrun: true }) // foo-1
|
||||
slugger.slug('foo') // foo-1
|
||||
slugger.slugText('foo') // foo-2
|
||||
slugger.slug('foo', { dryrun: true }) // foo-2
|
||||
slugger.slug('foo') // foo-2
|
||||
...
|
||||
```
|
||||
|
@ -39,17 +39,11 @@ module.exports = class Slugger {
|
||||
|
||||
/**
|
||||
* Convert string to unique id
|
||||
* @param {object} options
|
||||
* @param {boolean} options.dryrun Generates the next unique slug without updating the internal accumulator.
|
||||
*/
|
||||
slug(value) {
|
||||
slug(value, options = {}) {
|
||||
const slug = this.serialize(value);
|
||||
return this.getNextSafeSlug(slug);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get slug text without incrementing accumulator
|
||||
*/
|
||||
slugText(value) {
|
||||
const slug = this.serialize(value);
|
||||
return this.getNextSafeSlug(slug, true);
|
||||
return this.getNextSafeSlug(slug, options.dryrun);
|
||||
}
|
||||
};
|
||||
|
@ -64,11 +64,17 @@ describe('Test slugger functionality', () => {
|
||||
expect(slugger.slug('<em>html</em>')).toBe('html');
|
||||
});
|
||||
|
||||
it('should not increment seen when just getting text', () => {
|
||||
it('should not increment seen when using dryrun option', () => {
|
||||
const slugger = new marked.Slugger();
|
||||
slugger.slugText('<h1>This Section</h1>');
|
||||
slugger.slug('<h1>This Section</h1>', { dryrun: true });
|
||||
expect(slugger.slug('<h1>This Section</h1>')).toBe('this-section');
|
||||
});
|
||||
|
||||
it('should still return the next unique id when using dryrun', () => {
|
||||
const slugger = new marked.Slugger();
|
||||
expect(slugger.slug('<h1>This Section</h1>')).toBe('this-section');
|
||||
expect(slugger.slug('<h1>This Section</h1>', { dryrun: true })).toBe('this-section-1');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Test paragraph token type', () => {
|
||||
|
Loading…
Reference in New Issue
Block a user