Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save adnanrahic/adec3f9053064c28f4ba424c0909d824 to your computer and use it in GitHub Desktop.
Save adnanrahic/adec3f9053064c28f4ba424c0909d824 to your computer and use it in GitHub Desktop.
function generateSections(content) {
var sections = [];
var section = {};
var subsection = {};
var contentSplit = content.split('<h1>');
for(index in contentSplit) {
if (contentSplit[index].indexOf('</h1>') !== -1) {
section = {};
section.title = contentSplit[index].substring(0, contentSplit[index].indexOf('</h1>'));
contentSecondSplit = contentSplit[index].split('<h2>');
for(index in contentSecondSplit) {
if (contentSecondSplit[index].indexOf('</h2>') !== -1) {
section.subsections = section.subsections || [];
subsection = {};
subsection.title = contentSecondSplit[index].substring(0, contentSecondSplit[index].indexOf('</h2>'));
section.subsections.push(subsection);
}
}
sections.push(section);
}
}
return sections;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment