File tree Expand file tree Collapse file tree 2 files changed +55
-0
lines changed Expand file tree Collapse file tree 2 files changed +55
-0
lines changed Original file line number Diff line number Diff line change 1+ var _ = require ( 'lodash' ) ;
2+
3+ function isExercise ( nodes ) {
4+ var codeType = { type : 'code' } ;
5+
6+ // Number of code nodes in section
7+ var len = _ . filter ( nodes , codeType ) . length ;
8+
9+ return (
10+ // Got 3 or 4 code blocks
11+ ( len === 3 || len === 4 ) &&
12+ // Ensure all nodes are at the end
13+ _ . all ( _ . last ( nodes , len ) , codeType )
14+ ) ;
15+ }
16+
17+ module . exports = isExercise ;
Original file line number Diff line number Diff line change 1+ var _ = require ( 'lodash' ) ;
2+
3+ // Split a page up into sections (lesson, exercises, ...)
4+ function split ( nodes ) {
5+ var section = [ ] ;
6+
7+ return _ ( nodes )
8+ . reduce ( , function ( sections , el ) {
9+ if ( el . type === 'hr' ) {
10+ sections . push ( section ) ;
11+ section = [ ] ;
12+ section . links = nodes . links || { } ;
13+ } else {
14+ section . push ( el ) ;
15+ }
16+
17+ return sections ;
18+ } , [ ] )
19+ // Add remaining nodes
20+ . concat ( [ section ] )
21+ // Exclude empty sections
22+ . filter ( _ . negate ( _ . isEmpty ) )
23+ // Spit out JS array
24+ . value ( ) ;
25+ }
26+
27+ function join ( sections ) {
28+ var nodes = _ . reduce ( sections , function ( nodes , section ) {
29+ return nodes . concat ( section ) ;
30+ } , [ ] ) ;
31+ nodes . links = ( sections [ 0 ] && ( sections [ 0 ] . links ) || { } ;
32+ return nodes ;
33+ }
34+
35+ module . exports = {
36+ split : split ,
37+ join : split ,
38+ } ;
You can’t perform that action at this time.
0 commit comments