-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(search): Searching in the text of each section
closes #2
- Loading branch information
Showing
5 changed files
with
147 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
var la = require('lazy-ass'); | ||
var check = require('check-more-types'); | ||
var marked = require('marked'); | ||
|
||
/* global describe, it */ | ||
|
||
var toTokens = marked.lexer.bind(marked); | ||
|
||
describe('to-sections', function () { | ||
var toSections = require('./to-sections'); | ||
|
||
var text = [ | ||
'# 1 is foo', | ||
'foo is awesome', | ||
'# 2 is bar', | ||
'bar is not as good' | ||
].join('\n'); | ||
var tokens = toTokens(text); | ||
la(check.array(tokens)); | ||
|
||
it('is a function', function () { | ||
la(check.fn(toSections)); | ||
}); | ||
|
||
it('splits into 2 sections', function () { | ||
var sections = toSections(tokens); | ||
la(check.array(sections), sections); | ||
la(sections.length === 2, 'found 2 sections', sections); | ||
}); | ||
|
||
it('first section has 2 items', function () { | ||
var sections = toSections(tokens); | ||
var first = sections[0]; | ||
la(check.array(first), 'missing first section', first); | ||
la(first.length === 2, 'first has 2 tokens', first); | ||
}); | ||
|
||
it('first section has links', function () { | ||
var sections = toSections(tokens); | ||
var first = sections[0]; | ||
la(first.links, 'first section is missing links', first); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
var la = require('lazy-ass'); | ||
var check = require('check-more-types'); | ||
|
||
function toSections(tokens) { | ||
la(check.array(tokens), 'expected markdown tokens list', tokens); | ||
|
||
var foundSections = [], current; | ||
tokens.forEach(function (token) { | ||
if (token.type === 'heading') { | ||
if (current) { | ||
foundSections.push(current); | ||
} | ||
current = [token]; | ||
return; | ||
} else if (current) { | ||
current.push(token); | ||
} | ||
}); | ||
if (current) { | ||
foundSections.push(current); | ||
} | ||
|
||
foundSections.forEach(function (section) { | ||
section.links = tokens.links; | ||
}); | ||
return foundSections; | ||
} | ||
|
||
module.exports = toSections; |