From f2d3c5d2ed85d3924666a00caa8a28549d2ba292 Mon Sep 17 00:00:00 2001 From: Samy Pesse Date: Thu, 28 May 2015 14:38:21 +0200 Subject: [PATCH 1/2] Add test for #768 --- test/books/glossary/GLOSSARY.md | 4 ++++ test/books/glossary/README.md | 2 ++ test/glossary.js | 11 ++++++++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/test/books/glossary/GLOSSARY.md b/test/books/glossary/GLOSSARY.md index fa706f18d..18840d246 100644 --- a/test/books/glossary/GLOSSARY.md +++ b/test/books/glossary/GLOSSARY.md @@ -7,3 +7,7 @@ Just a simple and easy to understand test. ## hakunamatata This word is not present in the content. + +## test long + +This is a test with a longer text that the first entry to test order. diff --git a/test/books/glossary/README.md b/test/books/glossary/README.md index 50e8d4c04..d46da8bd2 100644 --- a/test/books/glossary/README.md +++ b/test/books/glossary/README.md @@ -6,3 +6,5 @@ The word test should be replaced by a glossary link. But the word test should not be replaced in code blocks ``` +The two words test long should be replaced by the correct glossary links. + diff --git a/test/glossary.js b/test/glossary.js index 631967e7b..14c3cf7f6 100644 --- a/test/glossary.js +++ b/test/glossary.js @@ -14,7 +14,7 @@ describe('Glossary', function () { it('should correctly list items', function() { book.should.have.property("glossary"); - book.glossary.should.have.lengthOf(2); + book.glossary.should.have.lengthOf(3); }); }); @@ -73,6 +73,15 @@ describe('Glossary', function () { } }); }); + + it('should correctly select the longest term', function() { + readme.should.be.html({ + ".page-inner a[href='GLOSSARY.html#test-long']": { + count: 1, + text: "test long" + } + }); + }); }); }); }); From 32203f643825c12e7869006896b03f08cfaa43cc Mon Sep 17 00:00:00 2001 From: Samy Pesse Date: Thu, 28 May 2015 14:49:53 +0200 Subject: [PATCH 2/2] Sort glossary before applying it on pages --- lib/utils/page.js | 7 +++++-- test/glossary.js | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/utils/page.js b/lib/utils/page.js index 9b33b9619..f24013f1d 100644 --- a/lib/utils/page.js +++ b/lib/utils/page.js @@ -237,13 +237,16 @@ function normalizeHtml(src, options) { }); // Replace glossary terms - _.each(options.glossary, function(term) { + var glossary = _.sortBy(options.glossary, function(term) { + return -term.name.length; + }); + _.each(glossary, function(term) { var r = new RegExp( "\\b(" + pregQuote(term.name.toLowerCase()) + ")\\b" , 'gi' ); var includedInFiles = false; $("*").each(function() { // Ignore codeblocks - if (_.contains(["code", "pre"], this.name.toLowerCase())) return; + if (_.contains(["code", "pre", "a"], this.name.toLowerCase())) return; replaceText($, this, r, function(match) { // Add to files index in glossary diff --git a/test/glossary.js b/test/glossary.js index 14c3cf7f6..fb573d92a 100644 --- a/test/glossary.js +++ b/test/glossary.js @@ -76,7 +76,7 @@ describe('Glossary', function () { it('should correctly select the longest term', function() { readme.should.be.html({ - ".page-inner a[href='GLOSSARY.html#test-long']": { + ".page-inner a[href='GLOSSARY.html#test_long']": { count: 1, text: "test long" }