1
1
import { IOption } from './index' ;
2
-
3
- const highlight = require ( 'highlight.js' ) ;
4
-
5
- const renderHighlight = function ( source : string , lang ) : string {
6
- if ( ! ( lang && highlight . getLanguage ( lang ) ) ) {
7
- return '' ;
8
- }
9
- return highlight . highlight ( lang , source , true ) . value ;
10
- } ;
11
-
12
- const markdown = function ( source : string , options = { } as object ) : string {
13
- return require ( 'markdown-it' ) (
14
- 'default' ,
15
- Object . assign (
16
- {
17
- html : true ,
18
- xhtmlOut : true ,
19
- breaks : true ,
20
- linkify : true ,
21
- typographer : true ,
22
- highlight : renderHighlight ,
23
- } ,
24
- options ,
25
- ) ,
26
- ) . render ( source ) ;
27
- } ;
28
-
29
- const replace = function ( source : string ) : string {
30
- return source . replace ( / ( [ { } ] ) / g, "{'$1'}" ) ;
31
- } ;
2
+ import loadContent from './lib/loadContent' ;
32
3
33
4
export default function loader ( source : string ) {
34
5
const opts : IOption = {
35
6
// default opts
36
7
wrapper : 'section' ,
8
+ anchor : [ 'h1' , 'h2' , 'h3' ] ,
37
9
} ;
38
10
39
11
if ( this ) {
@@ -44,16 +16,23 @@ export default function loader(source: string) {
44
16
Object . assign ( opts , require ( 'loader-utils' ) . getOptions ( this ) ) ;
45
17
}
46
18
47
- const { wrapper, className, style, ...options } = opts ;
48
-
49
- const code : string = require ( 'xss' ) ( replace ( markdown ( source , options ) ) ) ;
19
+ const { anchor, wrapper, className, style, ...options } = opts ;
20
+ const rawHtml = loadContent ( source , {
21
+ markdown : options ,
22
+ anchor,
23
+ wrapper,
24
+ className,
25
+ style,
26
+ } ) ;
50
27
51
28
const component = `import React from 'react';
52
29
export default function() {
53
- return (< ${ opts . wrapper } className=" ${ className } " style={ ${ style } }> ${ code } </ ${ wrapper } > );
30
+ return (${ rawHtml } );
54
31
}` ;
55
32
56
- return require ( '@babel/core' ) . transform ( component , {
33
+ return require ( '@babel/core' ) . transformSync ( component , {
57
34
plugins : [ '@babel/plugin-transform-react-jsx' ] ,
35
+ babelrc : false ,
36
+ configFile : false ,
58
37
} ) . code ;
59
38
}
0 commit comments