-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: add script to fetch design system pages for storybook #558
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e7c0e76
docs: script to fetch design system docs for storybook
KaiVandivier 9a52743
docs: transform relative links to github links; improve script
KaiVandivier 00379a2
docs: add DS fetch script before SB build
KaiVandivier 9919a0f
chore: ignore fetched design system files
KaiVandivier 7bcf460
docs: sort design sys principles
KaiVandivier 9f76a8c
docs: add blurb about DS principles to for-readers
KaiVandivier 8699b08
docs: improve link transforms on same-page links and md pages
KaiVandivier b86d1a1
chore: add script dependencies
KaiVandivier 92a6864
refactor: make script compatible with Node v10 for Netlify
KaiVandivier 6a75177
refactor: fix url formatting
KaiVandivier 6a45f48
chore: fix regular github url base
KaiVandivier 49a1cc2
docs: make relative link regex match more lazily
KaiVandivier e135042
Merge branch 'master' into docs-fetch-design-system-pages-for-sb
KaiVandivier 0c14326
chore: readd ds principles to preview after merge
KaiVandivier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
const fs = require('fs').promises | ||
const path = require('path') | ||
const { URL } = require('url') | ||
const { startCase } = require('lodash') | ||
const fetch = require('node-fetch') | ||
|
||
/** | ||
* This regex matches relative links in markdown, like '![link](../images/image.png)' | ||
* | ||
* This will be used to transform relative links (and images) in the design system | ||
* pages into external links to the github repo so these pages don't need to serve | ||
* images and to enable linking to other design system pages. | ||
* | ||
* There is a negative lookahead assertion to exclude external links that include 'http' | ||
* or same-page links that start with '#' | ||
* | ||
* It also checks if the link is for an image or not, because they need different handling | ||
* The capture groups for the above link would be 1. '!' 2. 'link' and 3. '../images/image.png' | ||
* | ||
* Check it out here: https://regex101.com/r/y5o9Tf/6 | ||
* | ||
*/ | ||
const mdRelativeLinkRegex = /(!)?\[(.*?)\]\((?!http|#)(.*?)\)/g | ||
const rawGithubUrl = | ||
'https://raw.githubusercontent.com/dhis2/design-system/master/principles/' | ||
const regularGithubUrl = | ||
'https://github.com/dhis2/design-system/tree/master/principles/' | ||
|
||
// eslint-disable-next-line max-params | ||
const linkReplacer = (_, imageBang, text, linkPath) => { | ||
// Different links for images or markdown pages | ||
const githubUrl = imageBang ? rawGithubUrl : regularGithubUrl | ||
const prefix = imageBang || '' // '!' or '' | ||
const url = new URL(linkPath, githubUrl).href | ||
return `${prefix}[${text}](${url})` | ||
} | ||
|
||
const replaceRelativeLinksWithGithubLinks = text => | ||
text.replace(mdRelativeLinkRegex, linkReplacer) | ||
|
||
// Template for creating .stories.mdx file for storybook that imports from regular markdown file | ||
const mdxTemplate = basename => `import { Meta, Description } from '@storybook/addon-docs/blocks' | ||
import ${startCase(basename).replace(/ /g, '')} from './${basename}.md' | ||
|
||
<Meta title="Design System Principles/${startCase(basename)}" /> | ||
|
||
<Description>{${startCase(basename).replace(/ /g, '')}}</Description> | ||
` | ||
|
||
const outputDir = path.join('docs', 'design-system') | ||
|
||
const downloadAndProcessFiles = async filteredFiles => { | ||
// Create output dir if one doesn't exist yet | ||
await fs.mkdir(outputDir, { recursive: true }) | ||
return Promise.all( | ||
filteredFiles.map(async ({ name, download_url }) => { | ||
// Get file contents | ||
const text = await fetch(download_url).then(res => res.text()) | ||
// Transform relative links (see comments above) | ||
const processedText = replaceRelativeLinksWithGithubLinks(text) | ||
// Make a regular markdown file from contents | ||
await fs.writeFile(path.join(outputDir, name), processedText) | ||
// Make an MDX stories file that imports regular markdown | ||
const basename = path.basename(name, '.md') | ||
await fs.writeFile( | ||
path.join(outputDir, `${basename}.stories.mdx`), | ||
mdxTemplate(basename) | ||
) | ||
}) | ||
) | ||
} | ||
|
||
// Fetch design system 'principles' directory info from github | ||
fetch( | ||
'https://api.github.com/repos/dhis2/design-system/contents/principles?ref=master' | ||
) | ||
.then(res => res.json()) | ||
// Filter out non-markdown files | ||
.then(fileInfo => | ||
fileInfo.filter( | ||
({ name, type }) => type === 'file' && name.endsWith('.md') | ||
) | ||
) | ||
// Fetch files and create local ones for storybook | ||
.then(downloadAndProcessFiles) | ||
.then(() => | ||
console.log('Design system files downloaded & processed for storybook') | ||
) | ||
.catch(console.error) | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Catching the error and logging it to the console implies we only want to notify about this failure. I think for something like this, that does probably make sense: we can still showcase our components without including the design system files. However, this does imply that the storybook demo is still functional with these and I have not verified this.
Also, I think we should probably include some additional information about the context before printing this error to the console, i.e.:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point -- if there's something that could go wrong that would interfere with the rest of the storybook, it would be partially-made story files that could cause run-time errors. In that case, if there's an error, maybe the directory the design system files get copied into should get wiped to delete any malformed files