Skip to content

Instantly share code, notes, and snippets.

@yangshun
Created March 9, 2017 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yangshun/4d87a4757cf894723ec1c555916249d3 to your computer and use it in GitHub Desktop.
Save yangshun/4d87a4757cf894723ec1c555916249d3 to your computer and use it in GitHub Desktop.
(() => {
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