@@ -10,7 +10,15 @@ const path = require("path");
10
10
const { promisify } = require ( "util" ) ;
11
11
const readdir = promisify ( fs . readdir ) ;
12
12
const readFile = promisify ( fs . readFile ) ;
13
+
14
+ const slugify = require ( "@sindresorhus/slugify" ) ;
13
15
const YAML = require ( "yaml" ) ;
16
+ // Using Marked for now because it's more simple to use. And because this is
17
+ // done at build time it doesn't increase the bundle size.
18
+ const marked = require ( "marked" ) ;
19
+ const sanitize = require ( "sanitize-html" ) ;
20
+
21
+ const toHTML = ( content ) => sanitize ( marked ( content ) ) ;
14
22
15
23
module . exports = function ( api ) {
16
24
api . loadSource ( ( { addCollection, addMetadata } ) => {
@@ -43,13 +51,13 @@ module.exports = function (api) {
43
51
}
44
52
} ) ;
45
53
46
- const addNodesFromFile = async ( collection , file ) => {
54
+ const addNodesFromFile = async ( collection , file , fn = ( x ) => x ) => {
47
55
const contents = await readFile ( file , { encoding : "utf-8" } ) ;
48
56
const items = YAML . parse ( contents ) ;
49
- for ( const item of items ) collection . addNode ( item ) ;
57
+ for ( const item of items ) collection . addNode ( fn ( item ) ) ;
50
58
} ;
51
59
52
- api . loadSource ( async ( { addCollection } ) => {
60
+ api . loadSource ( async ( { addCollection, addSchemaTypes } ) => {
53
61
await addNodesFromFile (
54
62
addCollection ( "Category" ) ,
55
63
path . join ( __dirname , "data/categories.yml" )
@@ -58,6 +66,23 @@ module.exports = function (api) {
58
66
addCollection ( "Tag" ) ,
59
67
path . join ( __dirname , "data/tags.yml" )
60
68
) ;
69
+
70
+ // Add Schema manually to prevent errors from missing fields.
71
+ // See https://gridsome.org/docs/schema-api/
72
+ const termSchema = await readFile (
73
+ path . join ( __dirname , "schemas/term.graphql" ) ,
74
+ { encoding : "utf-8" }
75
+ ) ;
76
+ addSchemaTypes ( termSchema ) ;
77
+ await addNodesFromFile (
78
+ addCollection ( "Term" ) ,
79
+ path . join ( __dirname , "data/glossary.yml" ) ,
80
+ ( term ) => {
81
+ if ( ! term . id ) term . id = slugify ( term . term ) ;
82
+ if ( term . description ) term . description = toHTML ( term . description ) ;
83
+ return term ;
84
+ }
85
+ ) ;
61
86
} ) ;
62
87
63
88
api . createPages ( ( { createPage } ) => {
0 commit comments