-
-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(webpack): create webpack-loader to traverse MDAST
- Loading branch information
Showing
3 changed files
with
132 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict'; | ||
|
||
const mdx = require('@mdx-js/mdx'); | ||
const fusumaMdxPlugin = require('./fusumaMdxPlugin'); | ||
|
||
module.exports = function fusumaLoader(src) { | ||
const cb = this.async(); | ||
|
||
const result = mdx.sync(src, { | ||
remarkPlugins: [fusumaMdxPlugin] | ||
}); | ||
|
||
cb(null, result); | ||
}; |
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,110 @@ | ||
'use strict'; | ||
|
||
const visit = require('unist-util-visit'); | ||
const mdxAstToMdxHast = require('@mdx-js/mdx/mdx-ast-to-mdx-hast'); | ||
const { toJSX } = require('@mdx-js/mdx/mdx-hast-to-jsx'); | ||
const { parse } = require('@babel/parser'); | ||
|
||
function createFusumaProps(nodes) { | ||
const property = {}; | ||
|
||
nodes.forEach(({ type, value }) => { | ||
if (type === 'comment') { | ||
const v = value.trim(); | ||
|
||
if (v === 'contents') { | ||
property.contents = true; | ||
} | ||
if (v.slice(0, 4) === 'note') { | ||
// sanitize | ||
const escapeMap = { | ||
'&': '&', | ||
'<': '<', | ||
'>': '>', | ||
'"': '"', | ||
"'": ''' | ||
// '/': '/' | ||
}; | ||
|
||
const s = v | ||
.slice(4) | ||
.trim() | ||
.replace(/[&<>"']/gim, (m) => escapeMap[m]) | ||
.replace(/\n/g, '\\n'); | ||
|
||
property.note = s; | ||
} | ||
if (v.slice(0, 8) === 'classes:') { | ||
property.classes = v.slice(8).trim(); | ||
} | ||
if (v.slice(0, 13) === 'sectionTitle:') { | ||
property.sectionTitle = v.slice(13).trim(); | ||
} | ||
} | ||
}); | ||
|
||
return `{${Object.entries(property) | ||
.map(([key, value]) => `${key}: '${value}'`) | ||
.join(',')}}`; | ||
} | ||
|
||
function fusumaMdxPlugin() { | ||
return (tree, file) => { | ||
const slides = []; | ||
let slide = []; | ||
const res = { | ||
jsx: [], | ||
fusumaProps: [] | ||
}; | ||
|
||
tree.children.forEach((n) => { | ||
if (n.type === 'thematicBreak') { | ||
slides.push(slide); | ||
slide = []; | ||
} else { | ||
slide.push(n); | ||
} | ||
}); | ||
|
||
// push last slide | ||
slides.push(slide); | ||
|
||
slides.forEach((slide) => { | ||
const hash = mdxAstToMdxHast()({ | ||
type: 'root', | ||
children: slide | ||
}); | ||
const mdxJSX = toJSX(hash); | ||
|
||
// jsx variable is established, so we don't use babel/parser | ||
const jsx = mdxJSX.match(/<MDXLayout.+?>([\s\S]*)<\/MDXLayout>/m); | ||
|
||
if (jsx) { | ||
const template = ` | ||
(props) => ( | ||
<> | ||
${jsx[1]} | ||
</> | ||
)`; | ||
const fusumaProps = createFusumaProps(slide); | ||
|
||
res.jsx.push(template); | ||
res.fusumaProps.push(fusumaProps); | ||
} | ||
}); | ||
|
||
// we have to transform one src to an array because we separate slides using --- | ||
// Identifier 'slides' has already been declared | ||
tree.children.push({ | ||
type: 'export', | ||
default: false, | ||
value: ` | ||
import React from 'react'; | ||
import { mdx } from '@mdx-js/react'; | ||
export const slides = [${res.jsx.join(',\n')}]; | ||
export const fusumaProps = [${res.fusumaProps.join(',\n')}];` | ||
}); | ||
}; | ||
} | ||
|
||
module.exports = fusumaMdxPlugin; |
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