Skip to content

Commit 4e12cc4

Browse files
Decouple Markdown translation from Webpack plumbing
1 parent 1ad2c58 commit 4e12cc4

File tree

13 files changed

+310
-38
lines changed

13 files changed

+310
-38
lines changed

jest.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
2+
module.exports = {
3+
preset: 'ts-jest',
4+
testEnvironment: 'node',
5+
projects: ['<rootDir>/packages/*'],
6+
transform: {
7+
'.': 'ts-jest',
8+
},
9+
}

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"lint:css": "stylelint './**/*.js'",
1111
"lint:js": "eslint .",
1212
"lint:js:fix": "eslint . --fix",
13-
"format": "prettier --write \"**/*.{js,jsx,ts,tsx}\""
13+
"format": "prettier --write \"**/*.{js,jsx,ts,tsx}\"",
14+
"test": "jest"
1415
},
1516
"workspaces": [
1617
"packages/*",
@@ -26,6 +27,8 @@
2627
"@babel/preset-typescript": "7.10.1",
2728
"@testing-library/jest-dom": "^5.2.0",
2829
"@testing-library/react": "^10.0.1",
30+
"@types/jest": "^26.0.0",
31+
"@types/memory-fs": "^0.3.3",
2932
"@typescript-eslint/eslint-plugin": "3.8.0",
3033
"@typescript-eslint/parser": "3.8.0",
3134
"babel-eslint": "^10.1.0",
@@ -46,6 +49,7 @@
4649
"file-loader": "^6.0.0",
4750
"html-webpack-plugin": "^4.0.1",
4851
"jest": "^26.1.0",
52+
"memory-fs": "^0.5.0",
4953
"prettier": "2.0.5",
5054
"react": "^16.13.1",
5155
"react-dom": "^16.13.1",
@@ -55,6 +59,7 @@
5559
"stylelint-config-styled-components": "^0.1.1",
5660
"stylelint-order": "^4.0.0",
5761
"stylelint-processor-styled-components": "^1.10.0",
62+
"ts-jest": "^26.1.0",
5863
"webpack": "^4.42.1",
5964
"webpack-cli": "^3.3.11",
6065
"webpack-dev-server": "^3.10.3"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
module.exports = {
22
coverageDirectory: 'coverage',
33
testRegex: '(/__tests__/.*|(\\.|/)(test))\\.[jt]sx?$',
4+
preset: 'ts-jest',
5+
transform: {
6+
'.': 'ts-jest',
7+
},
48
}
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
import example from './ExampleContent.md'
1+
import importedExample from './ExampleContent.md'
22

3-
document.title = example.title
3+
document.title = importedExample.title
44

5-
document.body.innerHTML = example.html
5+
const { translateMarkdown } = require('../src/markdownLoader')
6+
7+
const literalSource = `
8+
# Begin first section
9+
This section was generated via \`translateMarkdown\`. The section below was \`import\`ed from a static Markdown source file.
10+
# End first section. Second section follows.
11+
`
12+
13+
translateMarkdown(literalSource).then(translatedLiteralExample => {
14+
document.body.innerHTML = [translatedLiteralExample.contents, importedExample.html].join(' ')
15+
})
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
2+
module.exports = {
3+
preset: 'ts-jest',
4+
transform: {
5+
'.': 'ts-jest',
6+
},
7+
testPathIgnorePatterns: ['index.ts'],
8+
}

packages/markdown-loader/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
"version": "1.0.0",
44
"license": "MIT",
55
"repository": {
6-
"type" : "git",
7-
"url" : "https://github.com/broadinstitute/gnomad-browser-toolkit.git",
6+
"type": "git",
7+
"url": "https://github.com/broadinstitute/gnomad-browser-toolkit.git",
88
"directory": "packages/markdown-loader"
99
},
1010
"homepage": "https://github.com/broadinstitute/gnomad-browser-toolkit#readme",
1111
"bugs": "https://github.com/broadinstitute/gnomad-browser-toolkit/issues",
1212
"main": "lib/cjs/markdownLoader.js",
13-
"files": ["lib"],
13+
"files": [
14+
"lib"
15+
],
1416
"publishConfig": {
1517
"access": "public"
1618
},
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`markdownLoader translateMarkdown returns promise to string of rendered HTML 1`] = `
4+
"<h1>This is a heading</h1>
5+
<p>Followed by a paragraph</p>
6+
<ul>
7+
<li>Some people</li>
8+
<li>overuse</li>
9+
<li>bulleted lists
10+
</li>
11+
</ul>
12+
"
13+
`;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This is a heading
2+
3+
Followed by a paragraph
4+
5+
* Some people
6+
* overuse
7+
* bulleted lists
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { translateMarkdown } from '../markdownLoader'
2+
3+
describe('markdownLoader', () => {
4+
test('translateMarkdown returns promise to string of rendered HTML', async done => {
5+
const exampleMarkdownSource = `
6+
# This is a heading
7+
8+
Followed by a paragraph
9+
10+
* Some people
11+
* overuse
12+
* bulleted lists
13+
`
14+
15+
translateMarkdown(exampleMarkdownSource).then((result: any) => {
16+
expect(result.contents).toMatchSnapshot()
17+
done()
18+
})
19+
})
20+
})

packages/markdown-loader/src/markdownLoader.js

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,39 +33,42 @@ const linkTransformer = () => tree => {
3333
}
3434
/* eslint-enable no-param-reassign */
3535

36-
module.exports = function markdownLoader(content) {
37-
const callback = this.async()
38-
36+
const translateMarkdown = markdownSource =>
3937
remark()
4038
.use(frontmatter)
4139
.use(extract, { yaml })
4240
.use(imageFinder)
4341
.use(linkTransformer)
4442
.use(html)
45-
.process(content)
46-
.then(vfile => {
47-
let output = `export default ${JSON.stringify({
48-
...vfile.data,
49-
html: vfile.contents,
50-
})};`
43+
.process(markdownSource)
5144

52-
const imports = []
45+
const markdownLoader = function (content) {
46+
const callback = this.async()
47+
translateMarkdown(content).then(vfile => {
48+
let output = `export default ${JSON.stringify({
49+
...vfile.data,
50+
html: vfile.contents,
51+
})};`
5352

54-
output = output.replace(/___MD_CONTENT_IMAGE_([0-9]+)___/g, (match, p1) => {
55-
const imageIndex = parseInt(p1, 10)
56-
const request = loaderUtils.stringifyRequest(
57-
this,
58-
loaderUtils.urlToRequest(vfile.images[imageIndex])
59-
)
60-
imports.push(`import ___MD_CONTENT_IMAGE_${imageIndex}__ from ${request}`)
53+
const imports = []
6154

62-
return `" + ___MD_CONTENT_IMAGE_${imageIndex}__ + "`
63-
})
55+
output = output.replace(/___MD_CONTENT_IMAGE_([0-9]+)___/g, (_match, p1) => {
56+
const imageIndex = parseInt(p1, 10)
57+
const request = loaderUtils.stringifyRequest(
58+
this,
59+
loaderUtils.urlToRequest(vfile.images[imageIndex])
60+
)
61+
imports.push(`import ___MD_CONTENT_IMAGE_${imageIndex}__ from ${request}`)
6462

65-
if (imports.length > 0) {
66-
output = `${imports.join(';')};${output}`
67-
}
63+
return `" + ___MD_CONTENT_IMAGE_${imageIndex}__ + "`
64+
})
6865

69-
callback(null, output)
70-
}, callback)
66+
if (imports.length > 0) {
67+
output = `${imports.join(';')};${output}`
68+
}
69+
callback(null, output)
70+
}, callback)
7171
}
72+
73+
module.exports = markdownLoader
74+
module.exports.translateMarkdown = translateMarkdown

0 commit comments

Comments
 (0)