-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
030accc
commit 9b07a95
Showing
3 changed files
with
52 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,39 @@ | ||
// Replace list copied from https://css-tricks.com/snippets/javascript/htmlentities-for-javascript/ | ||
const _escText = text => { | ||
return String(text) | ||
.replace(/&/g, '&') | ||
.replace(/</g, '<') | ||
.replace(/>/g, '>') | ||
.replace(/"/g, '"') | ||
} | ||
|
||
const _buildLink = ({id, text, children}, ul, flat) => { | ||
let nestedList = '' | ||
|
||
if (children.length > 0 && flat) { | ||
nestedList = children.map(c => _buildLink(c, ul, flat)) | ||
} else if (children.length > 0) { | ||
nestedList = BuildList(children, ul, flat) | ||
} | ||
|
||
if (id && text && flat) { | ||
return `<li><a href="#${id}">${_escText(text)}</a></li>${( | ||
nestedList || [] | ||
).join('')}` | ||
} else if (id && text) { | ||
return `<li><a href="#${id}">${_escText(text)}</a>${nestedList}</li>` | ||
} else { | ||
return nestedList | ||
} | ||
} | ||
|
||
const BuildList = (listItems, ul, flat) => { | ||
const listType = ul ? 'ul' : 'ol' | ||
const list = listItems | ||
.sort((a, b) => a.order - b.order) | ||
.map(li => _buildLink(li, ul, flat)) | ||
|
||
return list.length > 0 ? `<${listType}>${list.join('')}</${listType}>` : '' | ||
} | ||
|
||
module.exports = BuildList | ||
// Replace list copied from https://css-tricks.com/snippets/javascript/htmlentities-for-javascript/ | ||
const _escText = text => { | ||
return String(text) | ||
.replace(/&/g, '&') | ||
.replace(/</g, '<') | ||
.replace(/>/g, '>') | ||
.replace(/"/g, '"') | ||
} | ||
|
||
const _buildLink = ({id, text, children}, ul, flat) => { | ||
let nestedList = '' | ||
|
||
if (children.length > 0 && flat) { | ||
nestedList = children.map(c => _buildLink(c, ul, flat)) | ||
} else if (children.length > 0) { | ||
nestedList = BuildList(children, ul, flat) | ||
} | ||
|
||
if (id && text && flat) { | ||
return `<li><a href="#${id}">${_escText(text)}</a></li>${( | ||
nestedList || [] | ||
).join('')}` | ||
} else if (id && text) { | ||
return `<li><a href="#${id}">${_escText(text)}</a>${nestedList}</li>` | ||
} else { | ||
return nestedList | ||
} | ||
} | ||
|
||
const BuildList = (listItems, ul, flat) => { | ||
const listType = ul ? 'ul' : 'ol' | ||
const list = listItems | ||
.sort((a, b) => a.order - b.order) | ||
.map(li => _buildLink(li, ul, flat)) | ||
|
||
return list.length > 0 ? `<${listType}>${list.join('')}</${listType}>` : '' | ||
} | ||
|
||
module.exports = BuildList |