Skip to content

Commit 946883f

Browse files
committed
Merge pull request GitbookIO#772 from GitbookIO/feature/glossary_order
Fix GitbookIO#768: sort glossary, start with longest matchs
2 parents b1b7e31 + 32203f6 commit 946883f

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

lib/utils/page.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,16 @@ function normalizeHtml(src, options) {
237237
});
238238

239239
// Replace glossary terms
240-
_.each(options.glossary, function(term) {
240+
var glossary = _.sortBy(options.glossary, function(term) {
241+
return -term.name.length;
242+
});
243+
_.each(glossary, function(term) {
241244
var r = new RegExp( "\\b(" + pregQuote(term.name.toLowerCase()) + ")\\b" , 'gi' );
242245
var includedInFiles = false;
243246

244247
$("*").each(function() {
245248
// Ignore codeblocks
246-
if (_.contains(["code", "pre"], this.name.toLowerCase())) return;
249+
if (_.contains(["code", "pre", "a"], this.name.toLowerCase())) return;
247250

248251
replaceText($, this, r, function(match) {
249252
// Add to files index in glossary

test/books/glossary/GLOSSARY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ Just a simple and easy to understand test.
77
## hakunamatata
88

99
This word is not present in the content.
10+
11+
## test long
12+
13+
This is a test with a longer text that the first entry to test order.

test/books/glossary/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ The word test should be replaced by a glossary link.
66
But the word test should not be replaced in code blocks
77
```
88

9+
The two words test long should be replaced by the correct glossary links.
10+

test/glossary.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('Glossary', function () {
1414

1515
it('should correctly list items', function() {
1616
book.should.have.property("glossary");
17-
book.glossary.should.have.lengthOf(2);
17+
book.glossary.should.have.lengthOf(3);
1818
});
1919
});
2020

@@ -73,6 +73,15 @@ describe('Glossary', function () {
7373
}
7474
});
7575
});
76+
77+
it('should correctly select the longest term', function() {
78+
readme.should.be.html({
79+
".page-inner a[href='GLOSSARY.html#test_long']": {
80+
count: 1,
81+
text: "test long"
82+
}
83+
});
84+
});
7685
});
7786
});
7887
});

0 commit comments

Comments
 (0)