-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathindex.js
54 lines (49 loc) · 1.36 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const { getOptions } = require('loader-utils')
const readingTime = require('reading-time')
const emoji = require('remark-emoji')
const images = require('remark-images')
const textr = require('remark-textr')
const slug = require('remark-slug')
const mdx = require('@mdx-js/mdx')
const mdxTableOfContents = require('mdx-table-of-contents')
const mdxExportJSONByDefault = require('mdx-constant')
const grayMatter = require('gray-matter')
const typography = require('./typography')
const rehypePrism = require('./prism')
module.exports = async function(source) {
let result
const { data, content: mdxContent } = grayMatter(source);
const callback = this.async()
const options = Object.assign(
{
remarkPlugins: [
slug,
images,
emoji,
[textr, { plugins: [typography] }]
],
rehypePlugins: [
rehypePrism,
],
compilers: [
mdxTableOfContents,
mdxExportJSONByDefault('frontMatter', data)
]
},
getOptions(this),
{filepath: this.resourcePath}
);
try {
result = await mdx(mdxContent, options)
} catch(err) {
return callback(err)
}
const estimatedReadingTime = readingTime(source)
let code = `
import React from 'react'
import { mdx } from '@mdx-js/react'
export const readingTime = ${JSON.stringify(estimatedReadingTime)}
${result}
`
return callback(null, code)
}