Skip to content

Commit 7db935b

Browse files
committed
Fix repeated slugs
Fixes graphql#12
1 parent 4c095be commit 7db935b

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

site/_core/Header.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,14 @@ export function toSlug(string) {
3838
}
3939

4040
export default (props) => {
41-
var slug = toSlug(props.toSlug || props.children);
41+
var usedSlugs = props.usedSlugs || {};
42+
var append = '';
43+
var loopCount = 0;
44+
do {
45+
var slug = toSlug((props.toSlug || props.children) + append);
46+
append = '-' + (++loopCount);
47+
} while (usedSlugs[slug]);
48+
usedSlugs[slug] = slug;
4249
var Heading = 'h' + props.level;
4350
var url = props.url || '';
4451

site/_core/Marked.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,7 @@ function Parser(options) {
745745
this.tokens = [];
746746
this.token = null;
747747
this.options = options || marked.defaults;
748+
this.usedSlugs = {};
748749
}
749750

750751
/**
@@ -816,7 +817,7 @@ Parser.prototype.tok = function() {
816817
}
817818
case 'heading': {
818819
return (
819-
<Header url={this.options.url} level={this.token.depth} toSlug={this.token.text}>
820+
<Header url={this.options.url} level={this.token.depth} toSlug={this.token.text} usedSlugs={this.usedSlugs}>
820821
{this.inline.output(this.token.text)}
821822
</Header>
822823
);

0 commit comments

Comments
 (0)