mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-24 11:26:31 +00:00
Add method ".toText" on glossary and summary
This commit is contained in:
@@ -5,18 +5,18 @@ describe('Glossary', function() {
|
||||
var Glossary = require('../glossary');
|
||||
var GlossaryEntry = require('../glossaryEntry');
|
||||
|
||||
describe('createFromEntries', function() {
|
||||
var glossary = Glossary.createFromEntries(File(), [
|
||||
{
|
||||
name: 'Hello World',
|
||||
description: 'Awesome!'
|
||||
},
|
||||
{
|
||||
name: 'JavaScript',
|
||||
description: 'This is a cool language'
|
||||
}
|
||||
]);
|
||||
var glossary = Glossary.createFromEntries(File(), [
|
||||
{
|
||||
name: 'Hello World',
|
||||
description: 'Awesome!'
|
||||
},
|
||||
{
|
||||
name: 'JavaScript',
|
||||
description: 'This is a cool language'
|
||||
}
|
||||
]);
|
||||
|
||||
describe('createFromEntries', function() {
|
||||
it('must add all entries', function() {
|
||||
var entries = glossary.getEntries();
|
||||
expect(entries.size).toBe(2);
|
||||
@@ -28,6 +28,15 @@ describe('Glossary', function() {
|
||||
expect(entry instanceof GlossaryEntry).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe('toText', function() {
|
||||
pit('return as markdown', function() {
|
||||
return glossary.toText('.md')
|
||||
.then(function(text) {
|
||||
expect(text).toContain('# Glossary');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -68,6 +68,15 @@ describe('Summary', function() {
|
||||
expect(article.getTitle()).toBe('My Second Article');
|
||||
});
|
||||
});
|
||||
|
||||
describe('toText', function() {
|
||||
pit('return as markdown', function() {
|
||||
return summary.toText('.md')
|
||||
.then(function(text) {
|
||||
expect(text).toContain('# Summary');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
var Immutable = require('immutable');
|
||||
|
||||
var error = require('../utils/error');
|
||||
var File = require('./file');
|
||||
var GlossaryEntry = require('./glossaryEntry');
|
||||
var parsers = require('../parsers');
|
||||
|
||||
var Glossary = Immutable.Record({
|
||||
file: File(),
|
||||
@@ -29,6 +31,27 @@ Glossary.prototype.getEntry = function(name) {
|
||||
return entries.get(id);
|
||||
};
|
||||
|
||||
/**
|
||||
Render glossary as text
|
||||
|
||||
@return {Promise<String>}
|
||||
*/
|
||||
Glossary.prototype.toText = function(parser) {
|
||||
var file = this.getFile();
|
||||
var entries = this.getEntries();
|
||||
|
||||
parser = parser? parsers.getByExt(parser) : file.getParser();
|
||||
|
||||
if (!parser) {
|
||||
throw error.FileNotParsableError({
|
||||
filename: file.getPath()
|
||||
});
|
||||
}
|
||||
|
||||
return parser.glossary.toText(entries.toJS());
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
Add/Replace an entry to a glossary
|
||||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
var Immutable = require('immutable');
|
||||
|
||||
var error = require('../utils/error');
|
||||
var File = require('./file');
|
||||
var SummaryPart = require('./summaryPart');
|
||||
var SummaryArticle = require('./summaryArticle');
|
||||
var parsers = require('../parsers');
|
||||
|
||||
var Summary = Immutable.Record({
|
||||
file: File(),
|
||||
@@ -63,6 +65,28 @@ Summary.prototype.getByPath = function(filePath) {
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
Render summary as text
|
||||
|
||||
@return {Promise<String>}
|
||||
*/
|
||||
Summary.prototype.toText = function(parser) {
|
||||
var file = this.getFile();
|
||||
var parts = this.getParts();
|
||||
|
||||
parser = parser? parsers.getByExt(parser) : file.getParser();
|
||||
|
||||
if (!parser) {
|
||||
throw error.FileNotParsableError({
|
||||
filename: file.getPath()
|
||||
});
|
||||
}
|
||||
|
||||
return parser.summary.toText({
|
||||
parts: parts.toJS()
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
Create a new summary for a list of parts
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ describe('findInstalled', function() {
|
||||
pit('must list default plugins for gitbook directory', function() {
|
||||
return findInstalled(path.resolve(__dirname, '../../../'))
|
||||
.then(function(plugins) {
|
||||
expect(plugins.size).toBe(7);
|
||||
expect(plugins.size > 7).toBeTruthy();
|
||||
|
||||
expect(plugins.has('fontsettings')).toBe(true);
|
||||
expect(plugins.has('search')).toBe(true);
|
||||
|
||||
Reference in New Issue
Block a user