Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(() => {
const storySubtitle = $$('.sub.header')[0].textContent;
const storyTitle = $$('h1.header')[0].textContent.replace(storySubtitle, '');
const chapters = [];
[...$$('div[data-reactroot]')[0].children[1].children[0].children[1].children].forEach(chapterEl => {
const chapterObject = { title: null, sections: [] };
const chapterChildren = chapterEl.children[0].children;
chapterObject.title = chapterChildren[0].textContent;
const sectionsEls = [...chapterChildren].slice(1);
sectionsEls.forEach(sectionEl => {
const sectionObject = {};
const sectionHeading = sectionEl.children[0].querySelector('.ui.header');
if (sectionHeading) {
sectionObject.title = sectionHeading.textContent;
}
const sectionSubheading = sectionEl.children[0].querySelector('p');
if (sectionSubheading) {
sectionObject.subtitle = sectionSubheading.textContent;
}
chapterObject.sections.push(sectionObject);
});
chapters.push(chapterObject);
});
let chaptersTemplate = '';
chapters.forEach(chapter => {
let sectionsTemplate = '';
chapter.sections.forEach(section => {
let titleLine = ``;
if (section.title) {
titleLine = `
title: '${section.title}',`;
}
let subtitleLine = ``;
if (section.subtitle) {
subtitleLine = `
subtitle: '${section.subtitle}',`;
}
sectionsTemplate += `
{ ${titleLine} ${subtitleLine}
sectionFn: () => {},
options: defaultSectionOptions,
},`;
});
chaptersTemplate += `
{
title: '${chapter.title}',
sections: [
${sectionsTemplate.trim()}
],
},`;
});
let template = `
export default storiesOf('Category', module)
.addWithChapters(
'${storyTitle}',
{
subtitle: '${storySubtitle}',
chapters: [
${chaptersTemplate.trim()}
],
},
);
`;
const textarea = document.createElement('textarea');
textarea.value = template;
document.querySelector('body').appendChild(textarea);
textarea.select();
document.execCommand('Copy');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.