1+ const fs = require ( 'fs' ) ;
12const path = require ( 'path' ) ;
23const { makeTemplateId, stringifyJSON } = require ( '../../Utils' ) ;
3- const { loadModule } = require ( '../../../Common/FileUtils' ) ;
4+ const { loadModule, resolveFile } = require ( '../../../Common/FileUtils' ) ;
5+ const MarkdownFilter = require ( '../../PreprocessorFilters/markdown' ) ;
6+
7+ const markdownExtension = function ( nunjucks , { viewPaths } ) {
8+ const name = 'includeMarkdown' ;
9+
10+ const markdownFilterOptions = {
11+ highlight : {
12+ use : {
13+ module : 'prismjs' ,
14+ options : {
15+ verbose : true , // display loaded dependencies
16+ } ,
17+ } ,
18+ } ,
19+ } ;
20+
21+ function IncludeMarkdown ( ) {
22+ this . tags = [ name ] ;
23+
24+ this . parse = function ( parser , nodes ) {
25+ const tok = parser . nextToken ( ) ;
26+ const args = parser . parseSignature ( null , true ) ;
27+ parser . advanceAfterBlockEnd ( tok . value ) ;
28+ return new nodes . CallExtension ( this , 'run' , args ) ;
29+ } ;
30+
31+ this . run = function ( context , filePath ) {
32+ const file = resolveFile ( filePath , { fs, paths : viewPaths , extensions : [ '.md' ] } ) ;
33+
34+ if ( ! file ) {
35+ throw new Error ( `Could not find the include file '${ filePath } '` ) ;
36+ }
37+
38+ const raw = fs . readFileSync ( file , 'utf8' ) ;
39+ const html = MarkdownFilter . getInstance ( markdownFilterOptions ) . apply ( raw ) ;
40+
41+ return new nunjucks . runtime . SafeString ( html ) ;
42+ } ;
43+ }
44+
45+ return {
46+ name,
47+ extension : new IncludeMarkdown ( ) ,
48+ } ;
49+ } ;
450
551// node module name
652const moduleName = 'nunjucks' ;
753
854const preprocessor = ( loaderContext , options = { } , { esModule, watch } ) => {
955 const nunjucks = loadModule ( moduleName ) ;
10- const env = new nunjucks . Environment ( ) ;
56+ const { FileSystemLoader, Environment, runtime } = nunjucks ;
57+
1158 const { rootContext } = loaderContext ;
1259 const viewPaths = ( options . views = [ ...new Set ( [ ...( options . views || [ ] ) , rootContext ] ) ] ) ;
1360 const async = options ?. async === true ;
@@ -27,8 +74,18 @@ const preprocessor = (loaderContext, options = {}, { esModule, watch }) => {
2774 nunjucks . installJinjaCompat ( ) ;
2875 }
2976
30- // set root template dirs, see the options https://mozilla.github.io/nunjucks/api.html#configure
31- nunjucks . configure ( viewPaths , options ) ;
77+ const env = new Environment (
78+ // set root template dirs, see the options https://mozilla.github.io/nunjucks/api.html#configure
79+ new FileSystemLoader ( viewPaths , options ) ,
80+ options
81+ ) ;
82+
83+ // register custom extensions
84+ const extensions = [ markdownExtension ( nunjucks , { viewPaths } ) ] ;
85+ extensions . forEach ( ( { name, extension } ) => {
86+ env . addExtension ( name , extension ) ;
87+ // console.log('[nunjucks] added extension:', name);
88+ } ) ;
3289
3390 return {
3491 /**
@@ -48,12 +105,12 @@ const preprocessor = (loaderContext, options = {}, { esModule, watch }) => {
48105 render : async
49106 ? ( content , { data } ) =>
50107 new Promise ( ( resolve , reject ) => {
51- nunjucks . renderString ( content , data , ( error , result ) => {
108+ env . renderString ( content , data , ( error , result ) => {
52109 if ( ! error ) resolve ( result ) ;
53110 else reject ( error ) ;
54111 } ) ;
55112 } )
56- : ( source , { data = { } } ) => nunjucks . renderString ( source , data ) ,
113+ : ( content , { data = { } } ) => env . renderString ( content , data ) ,
57114
58115 /**
59116 * Compile template into template function.
0 commit comments