Skip to content

Commit

Permalink
Parse _Sidebar.md line by line
Browse files Browse the repository at this point in the history
* This supports multiple formats in one file
* If a line doesn't match either format it is copied over instead of throwing an error
  • Loading branch information
amad-person committed Mar 29, 2019
1 parent 538ce57 commit 7910455
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/Site.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,29 +521,31 @@ Site.prototype.addSiteNavToDefaultLayout = function () {
Site.prototype.parseSidebarFileContent = function (rawSiteNavContent) {
let siteNavContent = '';
const doubleSquareBracketsFormatRegex = /\[\[([\w-]*)(\|)?([\w-]*)?\]\]/g;
const markdownLinkFormatRegex = /(?:__|[*#])|\[(.*?)\]\(.*?\)/g;
const markdownLinkFormatRegex = /\[(.*?)\]\((.*?)\)/g;
const baseUrlSubst = '{{baseUrl}}';
if (rawSiteNavContent.match(doubleSquareBracketsFormatRegex)) {
rawSiteNavContent.split('\n').forEach((navLine) => {
rawSiteNavContent.split('\n').forEach((navLine) => {
if (navLine.match(doubleSquareBracketsFormatRegex)) {
const navLineGroups = doubleSquareBracketsFormatRegex.exec(navLine);
const name = navLineGroups[1];
const link = navLineGroups[2] === undefined ? '' : navLineGroups[3];
const newNavLine = link === ''
? `[${name}](${baseUrlSubst}/${name}.html)` : `[${name}](${baseUrlSubst}/${link}.html)`;
? `[${name}](${baseUrlSubst}/${name.replace(/\s/g, '-')}.html)`
: `[${name}](${baseUrlSubst}/${link.replace(/\s/g, '-')}.html)`;
const finalNavLine = navLine.replace(doubleSquareBracketsFormatRegex, newNavLine);
siteNavContent += `${finalNavLine}\n`;
});
} else if (rawSiteNavContent.match(markdownLinkFormatRegex)) {
siteNavContent = rawSiteNavContent;
const openingBracketsRegex = /\([.]/g;
siteNavContent = siteNavContent.replace(openingBracketsRegex, baseUrlSubst);
const closingBracketsRegex = /\)/g;
const extSubst = '.html)';
siteNavContent = siteNavContent.replace(closingBracketsRegex, extSubst).replace('/.html', '/');
} else {
return Promise.reject(new Error('The current format in _Sidebar.md is not supported.'
+ ' A default site navigation menu will be created instead.'));
}
} else if (navLine.match(markdownLinkFormatRegex)) {
const navLineGroups = markdownLinkFormatRegex.exec(navLine);
const name = navLineGroups[1];
const link = navLineGroups[2];
const newNavLine = link === ''
? `[${name}](${baseUrlSubst}/${name.replace(/\s/g, '-')}.html)`
: `[${name}](${baseUrlSubst}/${link.replace('./', '').replace(/\s/g, '-')}.html)`;
const finalNavLine = navLine.replace(markdownLinkFormatRegex, newNavLine);
siteNavContent += `${finalNavLine}\n`;
} else {
siteNavContent += `${navLine}\n`;
}
});
return siteNavContent;
};

Expand Down

0 comments on commit 7910455

Please sign in to comment.