mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-16 15:45:13 +00:00
Add readme parsing for extracting title and description
This commit is contained in:
@@ -5,4 +5,5 @@ module.exports = {
|
||||
lex: require('./lex'),
|
||||
progress: require('./progress'),
|
||||
navigation: require('./navigation'),
|
||||
readme: require('./readme')
|
||||
};
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
var _ = require('lodash');
|
||||
var marked = require('marked');
|
||||
|
||||
function extractFirstNode(nodes, nType) {
|
||||
return _.chain(nodes)
|
||||
.filter(function(node) {
|
||||
return node.type == nType;
|
||||
})
|
||||
.pluck("text")
|
||||
.first()
|
||||
.value();
|
||||
}
|
||||
|
||||
|
||||
function parseReadme(src) {
|
||||
var nodes, title, description;
|
||||
|
||||
// Parse content
|
||||
nodes = marked.lexer(src);
|
||||
|
||||
var title = extractFirstNode(nodes, "heading");
|
||||
var description = extractFirstNode(nodes, "paragraph");
|
||||
|
||||
return {
|
||||
title: title,
|
||||
description: description
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
// Exports
|
||||
module.exports = parseReadme;
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
# This is the title
|
||||
|
||||
This is the book description.
|
||||
|
||||
other content
|
||||
...
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var assert = require('assert');
|
||||
|
||||
var readme = require('../').parse.readme;
|
||||
|
||||
|
||||
var CONTENT = fs.readFileSync(path.join(__dirname, './fixtures/README.md'), 'utf8');
|
||||
var LEXED = readme(CONTENT);
|
||||
|
||||
describe('Readme parsing', function () {
|
||||
|
||||
it('should contain a title', function() {
|
||||
assert(LEXED.title);
|
||||
});
|
||||
|
||||
it('should contain a description', function() {
|
||||
assert(LEXED.description);
|
||||
});
|
||||
|
||||
it('should extract the right title', function() {
|
||||
assert.equal(LEXED.title, "This is the title");
|
||||
});
|
||||
|
||||
it('should extract the right description', function() {
|
||||
assert.equal(LEXED.description, "This is the book description.");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user