-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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] Use custom webpack loader for markdown #26774
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
313af26
proof-of-concept: demo-loader
eps1lon ee6d809
Apply to bigger page
eps1lon aee5731
demo-loader -> @material-ui/demo-loader
eps1lon 4cb69e5
REVERT LATER build all locales
eps1lon d1973e6
fix lint errors
eps1lon 18c30b5
Convert to async loader
eps1lon 9ab504a
remove code todo since it's quite imminent
eps1lon 0c45819
Inline renderInlineMarkdown
eps1lon 0c157f4
Move markdown related things to a separate package
eps1lon b3e3af7
Parse markdown at bundle time
eps1lon 051fad5
Only load translated markdown versions eagerly
eps1lon 10cf381
Remove unused import in favor of this.addDependency
eps1lon ac97e31
Allow using one demo multiple times
eps1lon 1a6775e
Apply codemod
eps1lon 57775f8
Merge branch 'next' into docs/demo-loader
eps1lon a1e992e
demos are always evaluated in our fake require.context
eps1lon c37b6ec
Make sure TypeScript version of demo is included
eps1lon 8ab8bec
Merge remote-tracking branch 'upstream/next' into docs/demo-loader
eps1lon 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
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,127 @@ | ||
const { promises: fs, readFileSync } = require('fs'); | ||
const path = require('path'); | ||
const { notEnglishMarkdownRegExp, prepareMarkdown } = require('./parseMarkdown'); | ||
|
||
/** | ||
* @param {string} string | ||
*/ | ||
function upperCaseFirst(string) { | ||
return `${string[0].toUpperCase()}${string.slice(1)}`; | ||
} | ||
|
||
/** | ||
* @param {string} key | ||
* @example keyToJSIdentifier('index.md') === 'IndexMd' | ||
* @example keyToJSIdentifier('index-ja.md') === 'IndexJaMd' | ||
*/ | ||
function keyToJSIdentifier(key) { | ||
const delimiter = /(\.|-)/; | ||
return key | ||
.split(delimiter) | ||
.filter((part) => !delimiter.test(part)) | ||
.map(upperCaseFirst) | ||
.join(''); | ||
} | ||
|
||
/** | ||
* @example findTranslatedVersions('/a/b/index.md') === ['index-en.md', 'index-ja.md', 'index.md'] | ||
* @param {string} englishFilepath An absolute path to the english version written in markdown (.md) | ||
*/ | ||
async function findTranslatedVersions(englishFilepath) { | ||
const filename = path.basename(englishFilepath, '.md'); | ||
const files = await fs.readdir(path.dirname(englishFilepath)); | ||
|
||
// Given: index.md | ||
// Match: index-en.md, index-ja.md | ||
// Don't Match: otherindex-en.md, index-eng.md, index-e.md, index.tsx | ||
const translatedVersionRegExp = new RegExp(`^${filename}${notEnglishMarkdownRegExp.source}`); | ||
|
||
return files | ||
.filter((filepath) => { | ||
return translatedVersionRegExp.test(filepath); | ||
}) | ||
.concat(path.basename(englishFilepath)); | ||
} | ||
|
||
/** | ||
* @type {import('webpack').loader.Loader} | ||
*/ | ||
module.exports = async function demoLoader() { | ||
const rawKeys = await findTranslatedVersions(this.resourcePath); | ||
|
||
// TODO: Remove requireRaw mock (needs work in prepareMarkdown) | ||
const requireRaw = (key) => { | ||
const filepath = path.join(path.dirname(this.resourcePath), key); | ||
this.addDependency(filepath); | ||
return readFileSync(filepath, { encoding: 'utf-8' }); | ||
}; | ||
requireRaw.keys = () => rawKeys; | ||
const pageFilename = this.context.replace(this.rootContext, '').replace(/^\/src\/pages\//, ''); | ||
const { docs } = prepareMarkdown({ pageFilename, requireRaw }); | ||
|
||
const demos = {}; | ||
const demoKeys = []; | ||
new Set( | ||
docs.en.rendered | ||
.filter((markdownOrComponentConfig) => { | ||
return typeof markdownOrComponentConfig !== 'string' && markdownOrComponentConfig.demo; | ||
}) | ||
.map((demoConfig) => { | ||
return path.basename(demoConfig.demo); | ||
}), | ||
).forEach((filename) => { | ||
const demoName = `pages/${pageFilename}/${filename | ||
.replace(/\.\//g, '') | ||
.replace(/\.tsx/g, '.js')}`; | ||
|
||
demos[demoName] = { | ||
module: filename, | ||
raw: requireRaw(filename), | ||
}; | ||
demoKeys.push(filename); | ||
|
||
try { | ||
const moduleTS = filename.replace(/\.js$/, '.tsx'); | ||
const rawTS = requireRaw(moduleTS); | ||
|
||
demos[demoName].moduleTS = moduleTS; | ||
demos[demoName].rawTS = rawTS; | ||
demoKeys.push(moduleTS); | ||
} catch (error) { | ||
// TS version of the demo doesn't exist. This is fine. | ||
} | ||
}); | ||
|
||
/** | ||
* @param {string} key | ||
*/ | ||
function getRequireDemoIdentifier(key) { | ||
return keyToJSIdentifier(key); | ||
} | ||
|
||
const transformed = ` | ||
${demoKeys | ||
.map((key) => { | ||
return `import ${getRequireDemoIdentifier(key)} from './${key}';`; | ||
}) | ||
.join('\n')} | ||
|
||
export const docs = ${JSON.stringify(docs, null, 2)}; | ||
export const demos = ${JSON.stringify(demos, null, 2)}; | ||
export function requireDemo(module) { | ||
return { | ||
${demoKeys | ||
.map((key) => { | ||
// TODO: Remove ES module interop once all demos are loaded via loader | ||
// i.e. replace `{ default: ... }` with `...` | ||
return `'${key}': { default: ${getRequireDemoIdentifier(key)} }`; | ||
}) | ||
.join(',\n')} | ||
}[module]; | ||
} | ||
requireDemo.keys = () => { | ||
return ${JSON.stringify(demoKeys, null, 2)} | ||
}`; | ||
|
||
return transformed; | ||
}; |
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,17 @@ | ||
{ | ||
"name": "@material-ui/markdown", | ||
"version": "0.1.0", | ||
"private": true, | ||
"type": "commonjs", | ||
"main": "./parseMarkdown.js", | ||
"exports": { | ||
".": "./parseMarkdown.js", | ||
"./loader": "./loader.js", | ||
"./prism": "./prism.js" | ||
}, | ||
"dependencies": { | ||
"lodash": "^4.17.15", | ||
"marked": "^2.0.0", | ||
"prismjs": "^1.17.1" | ||
} | ||
} |
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
File renamed without changes.
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
2 changes: 1 addition & 1 deletion
2
docs/src/modules/utils/textToHash.test.js → docs/packages/markdown/textToHash.test.js
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,15 +1,7 @@ | ||
import * as React from 'react'; | ||
import TopLayoutBlog from 'docs/src/modules/components/TopLayoutBlog'; | ||
import { prepareMarkdown } from 'docs/src/modules/utils/parseMarkdown'; | ||
import { docs } from '!@material-ui/markdown/loader!./2019-developer-survey-results.md'; | ||
|
||
const pageFilename = 'blog/2019-developer-survey-results'; | ||
const requireRaw = require.context('!raw-loader!./', false, /2019-developer-survey-results\.md$/); | ||
|
||
export default function Page({ docs }) { | ||
export default function Page() { | ||
return <TopLayoutBlog docs={docs} />; | ||
} | ||
|
||
Page.getInitialProps = () => { | ||
const { demos, docs } = prepareMarkdown({ pageFilename, requireRaw }); | ||
return { demos, docs }; | ||
}; |
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,15 +1,7 @@ | ||
import * as React from 'react'; | ||
import TopLayoutBlog from 'docs/src/modules/components/TopLayoutBlog'; | ||
import { prepareMarkdown } from 'docs/src/modules/utils/parseMarkdown'; | ||
import { docs } from '!@material-ui/markdown/loader!./2019.md'; | ||
|
||
const pageFilename = 'blog/2019'; | ||
const requireRaw = require.context('!raw-loader!./', false, /2019\.md$/); | ||
|
||
export default function Page({ docs }) { | ||
export default function Page() { | ||
return <TopLayoutBlog docs={docs} />; | ||
} | ||
|
||
Page.getInitialProps = () => { | ||
const { demos, docs } = prepareMarkdown({ pageFilename, requireRaw }); | ||
return { demos, docs }; | ||
}; |
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,15 +1,7 @@ | ||
import * as React from 'react'; | ||
import TopLayoutBlog from 'docs/src/modules/components/TopLayoutBlog'; | ||
import { prepareMarkdown } from 'docs/src/modules/utils/parseMarkdown'; | ||
import { docs } from '!@material-ui/markdown/loader!./2020-developer-survey-results.md'; | ||
|
||
const pageFilename = 'blog/2020-developer-survey-results'; | ||
const requireRaw = require.context('!raw-loader!./', false, /2020-developer-survey-results\.md$/); | ||
|
||
export default function Page({ docs }) { | ||
export default function Page() { | ||
return <TopLayoutBlog docs={docs} />; | ||
} | ||
|
||
Page.getInitialProps = () => { | ||
const { demos, docs } = prepareMarkdown({ pageFilename, requireRaw }); | ||
return { demos, docs }; | ||
}; |
Oops, something went wrong.
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.
I'm taking care of it #26970.