Skip to content

Commit

Permalink
Merge pull request #351 from nusjzx/search-panel-headings
Browse files Browse the repository at this point in the history
Search panel headings
  • Loading branch information
yamgent authored Jul 24, 2018
2 parents 531a1bf + 090622f commit 56ea03c
Show file tree
Hide file tree
Showing 18 changed files with 140 additions and 7 deletions.
43 changes: 37 additions & 6 deletions lib/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,21 +165,53 @@ Page.prototype.prepareTemplateData = function () {
};

/**
* Records h1,h2,h3 headings into this.headings
* @param renderedPage a page with its headings rendered
* Records headings inside rendered page into this.headings
*/
Page.prototype.collectHeadings = function (renderedPage) {
const $ = cheerio.load(renderedPage);
Page.prototype.collectHeadings = function () {
this.collectHeadingsInContent(fs.readFileSync(this.resultPath));
};

/**
* Records headings inside content into this.headings
* @param content that contains the headings
*/
Page.prototype.collectHeadingsInContent = function (content) {
let $ = cheerio.load(content);
$('panel').each((index, panel) => {
if (panel.attribs.header) {
this.collectHeadingsInContent(md.render(panel.attribs.header));
}
}).filter((index, panel) => panel.attribs.expanded !== undefined)
.each((index, panel) => {
if (panel.attribs.src) {
const src = panel.attribs.src.split('#')[0];
const includePath = path.resolve(this.resultPath,
path.relative(this.sourcePath,
this.baseUrl
? path.relative(this.baseUrl, src)
: src.substring(1)));
const includeContent = fs.readFileSync(includePath);
if (panel.attribs.fragment) {
$ = cheerio.load(includeContent);
this.collectHeadingsInContent($(`#${panel.attribs.fragment}`).html());
} else {
this.collectHeadingsInContent(includeContent);
}
} else {
this.collectHeadingsInContent($(panel).html());
}
});
$ = cheerio.load(content);
if (this.headingIndexingLevel > 0) {
let headingsSelector = 'h1';
for (let i = 2; i <= this.headingIndexingLevel; i += 1) {
headingsSelector += `, h${i}`;
}
$('panel').remove();
$(headingsSelector).each((i, heading) => {
this.headings[$(heading).attr('id')] = $(heading).text();
});
}
return renderedPage;
};

/**
Expand Down Expand Up @@ -317,7 +349,6 @@ Page.prototype.generate = function (builtFiles) {
.then(result => markbinder.resolveBaseUrl(result, fileConfig))
.then(result => fs.outputFileAsync(this.tempPath, result))
.then(() => markbinder.renderFile(this.tempPath, fileConfig))
.then(result => this.collectHeadings(result))
.then((result) => {
this.content = htmlBeautify(result, { indent_size: 2 });

Expand Down
5 changes: 5 additions & 0 deletions lib/Site.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,11 @@ Site.prototype.generatePages = function () {
});
return new Promise((resolve, reject) => {
Promise.all(processingFiles)
.then(() => {
this.pages.forEach((page) => {
page.collectHeadings();
});
})
.then(resolve)
.catch(reject);
});
Expand Down
1 change: 1 addition & 0 deletions test/test_site/_markbind/boilerplates/boilerTestPanel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
### boilerplate test for panel heading
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
### heading in panel boilerplate
17 changes: 17 additions & 0 deletions test/test_site/expected/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,23 @@ <h2 id="feature-list">Feature list</h2>
<li>Timer – Additional fixed time restriction on the player.</li>
</ol>
<img src="/test_site/sub_site/images/I'm not allowed to use my favorite tool.png"></div>
<h1 id="panel-without-src">Panel without src</h1>
<panel header="## Panel without src header" expanded="">
<div>
<h3 id="panel-without-src-content-heading">panel without src content heading</h3>
</div>
</panel>
<h1 id="panel-with-normal-src">Panel with normal src</h1>
<panel header="## Panel with normal src header" src="/test_site\testPanels\PanelNormalSource._include_.html" expanded=""></panel>
<h1 id="panel-with-src-from-a-page-segment">Panel with src from a page segment</h1>
<panel header="## Panel with src from a page segment header" src="/test_site\testPanels\PanelSourceContainsSegment._include_.html#segment" expanded="" fragment="segment"></panel>
<h1 id="panel-with-boilerplate">Panel with boilerplate</h1>
<panel header="## boilerplate referencing" src="/test_site\testPanels\boilerTestPanel._include_.html" expanded=""></panel>
<panel header="## Referencing specified path in boilerplate" src="/test_site\testPanels\notInside._include_.html" expanded=""></panel>
<h1 id="nested-panel">Nested panel</h1>
<panel header="## Outer nested panel" src="/test_site\testPanels\NestedPanel._include_.html" expanded=""></panel>
<h1 id="panel-with-src-from-another-markbind-site">panel with src from another Markbind site</h1>
<panel header="## Panel with src from another Markbind site header" src="/test_site\sub_site\index._include_.html" expanded=""></panel>
</div>
</div>
</div>
Expand Down
21 changes: 20 additions & 1 deletion test/test_site/expected/siteData.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
"pages": [
{
"headings": {
"panel-without-src-header": "Panel without src header",
"panel-with-normal-src-header": "Panel with normal src header",
"panel-with-src-from-a-page-segment-header": "Panel with src from a page segment header",
"boilerplate-referencing": "boilerplate referencing",
"referencing-specified-path-in-boilerplate": "Referencing specified path in boilerplate",
"outer-nested-panel": "Outer nested panel",
"panel-with-src-from-another-markbind-site-header": "Panel with src from another Markbind site header",
"panel-without-src-content-heading": "panel without src content heading",
"panel-normal-source-content-headings": "panel normal source content headings",
"panel-source-segment-content-headings": "panel source segment content headings",
"boilerplate-test-for-panel-heading": "boilerplate test for panel heading",
"heading-in-panel-boilerplate": "heading in panel boilerplate",
"nested-panel": "Nested panel",
"normal-panel-content-heading": "normal panel content heading",
"feature-list": "Feature list",
"navigation": "Navigation",
"testing-site-nav": "Testing Site-Nav",
"variables-that-reference-another-variable": "Variables that reference another variable",
Expand All @@ -16,7 +31,11 @@
"nested-include": "Nested include",
"html-include": "HTML include",
"include-from-another-markbind-site": "Include from another Markbind site",
"feature-list": "Feature list"
"panel-without-src": "Panel without src",
"panel-with-normal-src": "Panel with normal src",
"panel-with-src-from-a-page-segment": "Panel with src from a page segment",
"panel-with-boilerplate": "Panel with boilerplate",
"panel-with-src-from-another-markbind-site": "panel with src from another Markbind site"
},
"title": "Hello World",
"footer": "footer.md",
Expand Down
11 changes: 11 additions & 0 deletions test/test_site/expected/sub_site/index._include_.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<p>This is a page from another Markbind site.</p>
<h2 id="feature-list">Feature list</h2>
<p>It is a list of features (or functionalities) grouped according to some criteria such as priority (e.g. must-have, nice-to-have, etc. ), order of delivery, object or process related (e.g. order-related, invoice-related, etc.). Here is a sample feature
list from Minesweeper (only a brief description has been provided to save space).</p>
<ol>
<li>Basic play – Single player play.</li>
<li>Difficulty levels – Additional Medium and Advanced levels.</li>
<li>Versus play – Two players can play against each other.</li>
<li>Timer – Additional fixed time restriction on the player.</li>
</ol>
<img src="/test_site/sub_site/images/I'm not allowed to use my favorite tool.png">
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<panel header="## Nested Panel" src="/test_site\testPanels\NormalPanelContent._include_.html" expanded=""></panel>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h3 id="normal-panel-content-heading">normal panel content heading</h3>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h3 id="panel-normal-source-content-headings">panel normal source content headings</h3>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div id="segment">
<h3 id="panel-source-segment-content-headings">panel source segment content headings</h3>
</div>
<h3 id="this-heading-is-not-src-of-any-panel-so-it-should-not-be-in-the-search-data">this heading is not src of any panel, so it should not be in the search data</h3>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h3 id="boilerplate-test-for-panel-heading">boilerplate test for panel heading</h3>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h3 id="heading-in-panel-boilerplate">heading in panel boilerplate</h3>
29 changes: 29 additions & 0 deletions test/test_site/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,33 @@ siteNav: site-nav.md
# Include from another Markbind site
<include src="sub_site/index.md" />

# Panel without src
<panel header="## Panel without src header" expanded>
<markdown>
### panel without src content heading
</markdown>
</panel>

# Panel with normal src
<panel header="## Panel with normal src header" src="testPanels/PanelNormalSource.md" expanded>
</panel>

# Panel with src from a page segment
<panel header="## Panel with src from a page segment header" src="testPanels/PanelSourceContainsSegment.md#segment" expanded>
</panel>

# Panel with boilerplate
<panel header="## boilerplate referencing" src="testPanels/boilerTestPanel.md" boilerplate expanded>
</panel>

<panel header="## Referencing specified path in boilerplate" src="testPanels/notInside.md" boilerplate="folder/panelBoilerplate.md" expanded>
</panel>

# Nested panel
<panel header="## Outer nested panel" src="testPanels/NestedPanel.md" expanded>
</panel>

# panel with src from another Markbind site
<panel header="## Panel with src from another Markbind site header" src="sub_site/index.md" expanded>
</panel>
</div>
2 changes: 2 additions & 0 deletions test/test_site/testPanels/NestedPanel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<panel header ="## Nested Panel" src="NormalPanelContent.md" expanded>
</panel>
1 change: 1 addition & 0 deletions test/test_site/testPanels/NormalPanelContent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
### normal panel content heading
1 change: 1 addition & 0 deletions test/test_site/testPanels/PanelNormalSource.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
### panel normal source content headings
6 changes: 6 additions & 0 deletions test/test_site/testPanels/PanelSourceContainsSegment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div id="segment">

### panel source segment content headings
</div>

### this heading is not src of any panel, so it should not be in the search data

0 comments on commit 56ea03c

Please sign in to comment.