Use real glossary name when annotating

This commit is contained in:
Samy Pesse
2016-05-26 21:10:06 +02:00
parent ecd86ec877
commit b47ebaa9d9
3 changed files with 13 additions and 7 deletions
+7 -2
View File
@@ -4,6 +4,7 @@ var Api = require('../api');
var Plugins = require('../plugins');
var Promise = require('../utils/promise');
var defaultBlocks = require('../constants/defaultBlocks');
var fileToOutput = require('./helper/fileToOutput');
var CODEBLOCK = 'code';
@@ -17,9 +18,13 @@ function getModifiers(output, page) {
var book = output.getBook();
var plugins = output.getPlugins();
var glossary = book.getGlossary();
var entries = glossary.getEntries();
var file = page.getFile();
// Glossary entries
var entries = glossary.getEntries();
var glossaryFile = glossary.getFile();
var glossaryFilename = fileToOutput(output, glossaryFile.getPath());
// Current file path
var currentFilePath = file.getPath();
@@ -44,7 +49,7 @@ function getModifiers(output, page) {
Modifiers.resolveImages.bind(null, currentFilePath),
// Annotate text with glossary entries
Modifiers.annotateText.bind(null, entries),
Modifiers.annotateText.bind(null, entries, glossaryFilename),
// Highlight code blocks using "code" block
Modifiers.highlightCode.bind(null, function(lang, source) {
@@ -12,7 +12,7 @@ describe('annotateText', function() {
it('should annotate text', function() {
var $ = cheerio.load('<p>This is a word, and multiple words</p>');
annotateText(entries, $);
annotateText(entries, 'GLOSSARY.md', $);
var links = $('a');
expect(links.length).toBe(2);
@@ -31,14 +31,14 @@ describe('annotateText', function() {
it('should not annotate scripts', function() {
var $ = cheerio.load('<script>This is a word, and multiple words</script>');
annotateText(entries, $);
annotateText(entries, 'GLOSSARY.md', $);
expect($('a').length).toBe(0);
});
it('should not annotate when has class "no-glossary"', function() {
var $ = cheerio.load('<p class="no-glossary">This is a word, and multiple words</p>');
annotateText(entries, $);
annotateText(entries, 'GLOSSARY.md', $);
expect($('a').length).toBe(0);
});
});
+3 -2
View File
@@ -62,9 +62,10 @@ function replaceText($, el, search, replace, text_only ) {
Annotate text using a list of GlossaryEntry
@param {List<GlossaryEntry>}
@param {String} glossaryFilePath
@param {HTMLDom} $
*/
function annotateText(entries, $) {
function annotateText(entries, glossaryFilePath, $) {
entries.forEach(function(entry) {
var entryId = entry.getID();
var name = entry.getName();
@@ -81,7 +82,7 @@ function annotateText(entries, $) {
) return;
replaceText($, this, searchRegex, function(match) {
return '<a href="/GLOSSARY.md#' + entryId + '" '
return '<a href="/' + glossaryFilePath + '#' + entryId + '" '
+ 'class="glossary-term" title="' + escape(description) + '">'
+ match
+ '</a>';