mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-10-03 20:20:35 +00:00
Add content & code generation for exercise sections
This commit is contained in:
+28
-9
@@ -33,6 +33,19 @@ function sectionType(nodes, idx) {
|
||||
return 'normal';
|
||||
}
|
||||
|
||||
// Render a section using our custom renderer
|
||||
function render(section) {
|
||||
// marked's Render expects this, we don't use it yet
|
||||
section.links = {};
|
||||
|
||||
// Build options using defaults and our custom renderer
|
||||
var options = _.extend({}, marked.defaults, {
|
||||
renderer: renderer()
|
||||
});
|
||||
|
||||
return marked.parser(section, options);
|
||||
}
|
||||
|
||||
function parsePage(src) {
|
||||
var nodes = marked.lexer(src);
|
||||
|
||||
@@ -45,23 +58,29 @@ function parsePage(src) {
|
||||
.map(function(section) {
|
||||
// Transform given type
|
||||
if(section.type === 'exercise') {
|
||||
var nonCodeNodes = _.reject(section, {
|
||||
'type': 'code'
|
||||
});
|
||||
|
||||
var codeNodes = _.filter(section, {
|
||||
'type': 'code'
|
||||
});
|
||||
|
||||
return {
|
||||
type: section.type,
|
||||
content: render(nonCodeNodes),
|
||||
code: {
|
||||
base: codeNodes[0].text,
|
||||
solution: codeNodes[1].text,
|
||||
validation: codeNodes[2].text,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// marked's Render expects this, we don't use it yet
|
||||
section.links = {};
|
||||
|
||||
// Build options using defaults and our custom renderer
|
||||
var options = _.extend({}, marked.defaults, {
|
||||
renderer: renderer()
|
||||
});
|
||||
|
||||
// Render normal pages
|
||||
return {
|
||||
type: section.type,
|
||||
content: marked.parser(section, options)
|
||||
content: render(section)
|
||||
};
|
||||
})
|
||||
.value();
|
||||
|
||||
+8
-1
@@ -21,7 +21,14 @@ describe('Page parsing', function() {
|
||||
|
||||
it('should gen content for normal sections', function() {
|
||||
assert(LEXED[0].content);
|
||||
assert(!LEXED[1].content);
|
||||
assert(LEXED[2].content);
|
||||
});
|
||||
|
||||
it('should gen code and content for exercise sections', function() {
|
||||
assert(LEXED[1].content);
|
||||
assert(LEXED[1].code);
|
||||
assert(LEXED[1].code.base);
|
||||
assert(LEXED[1].code.solution);
|
||||
assert(LEXED[1].code.validation);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user