mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-10-03 20:20:35 +00:00
Add method glossary.find
This commit is contained in:
@@ -2,6 +2,15 @@ var _ = require('lodash');
|
||||
var util = require('util');
|
||||
var BackboneFile = require('./file');
|
||||
|
||||
// Normalize a glossary entry name into a unique id
|
||||
function nameToId(name) {
|
||||
return name.toLowerCase()
|
||||
.replace(/[\/\\\?\%\*\:\;\|\"\'\\<\\>\#\$\(\)\!\.\@]/g, '')
|
||||
.replace(/ /g, '_')
|
||||
.trim();
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
A glossary entry is represented by a name and a short description
|
||||
An unique id for the entry is generated using its name
|
||||
@@ -19,10 +28,7 @@ function GlossaryEntry(name, description) {
|
||||
|
||||
// Normalizes a glossary entry's name to create an ID
|
||||
GlossaryEntry.prototype.getId = function() {
|
||||
return this.name.toLowerCase()
|
||||
.replace(/[\/\\\?\%\*\:\;\|\"\'\\<\\>\#\$\(\)\!\.\@]/g, '')
|
||||
.replace(/ /g, '_')
|
||||
.trim();
|
||||
return nameToId(this.name);
|
||||
};
|
||||
|
||||
|
||||
@@ -54,6 +60,11 @@ Glossary.prototype.get = function(id) {
|
||||
});
|
||||
};
|
||||
|
||||
// Find an entry by its name
|
||||
Glossary.prototype.find = function(name) {
|
||||
return this.get(nameToId(name));
|
||||
};
|
||||
|
||||
// Return false if glossary has entries (and exists)
|
||||
Glossary.prototype.isEmpty = function(id) {
|
||||
return _.size(this.entries) === 0;
|
||||
|
||||
+9
-14
@@ -55,22 +55,17 @@ describe('Glossary', function() {
|
||||
should.not.exist(entry);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse glossary entries', function() {
|
||||
return mock.setupDefaultBook({
|
||||
'GLOSSARY.md': '# Glossary\n\n### Hello World\n\nThis is an entry'
|
||||
})
|
||||
.then(function(book) {
|
||||
return book.prepareConfig()
|
||||
|
||||
.then(function() {
|
||||
return book.glossary.load();
|
||||
})
|
||||
.then(function() {
|
||||
|
||||
|
||||
describe('glossary.find', function() {
|
||||
it('should return an existing entry', function() {
|
||||
var entry = book.glossary.find('HeLLo World');
|
||||
should.exist(entry);
|
||||
entry.id.should.equal('hello_world');
|
||||
});
|
||||
|
||||
it('should undefined return non existing entry', function() {
|
||||
var entry = book.glossary.find('Hello');
|
||||
should.not.exist(entry);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user