Repository navigation
Expand file tree
/
Copy pathgatsby-node.js
More file actions
155 lines (143 loc) · 4.44 KB
/
Copy pathgatsby-node.js
File metadata and controls
155 lines (143 loc) · 4.44 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const _ = require('lodash');
const { createFilePath } = require(`gatsby-source-filesystem`);
const path = require("path");
var md = require('markdown-it');
var fa = require('markdown-it-fontawesome');
const fs = require('fs');
md().use(fa);
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === `MarkdownRemark`) {
const meta = getMetaFromPath(node);
if(meta){
createNodeField({ node, name: `lang`, value: meta.lang });
createNodeField({
node, name: `slug`,
value: meta.slug,
});
createNodeField({
node, name: `type`,
value: meta.type,
});
createNodeField({
node, name: `url`,
value: meta.type + "/" + meta.slug,
});
}
}
};
const buildTranslations = (nodes) => {
let translations = {};
nodes.forEach(({ node }) => {
const meta = getMetaFromPath(node);
if(typeof translations[meta.urlSlug] === 'undefined') translations[meta.urlSlug] = {};
translations[meta.urlSlug][meta.lang] = !meta.customSlug ? meta.lang+"/" : "";
translations[meta.urlSlug][meta.lang] += meta.type + "/" + meta.slug;
});
return translations;
};
// exports.onPostBuild = () => {
// fs.copySync(
// path.join(__dirname, "/src/locales"),
// path.join(__dirname, "/public/locales")
// );
// };
const getMetaFromPath = ({ fileAbsolutePath, frontmatter }) => {
const regex = /\/([\w-]*)\/([\w-\]\[]*)\.?(\w{1,2})?\.md/gm;
let m = regex.exec(fileAbsolutePath);
if(!m) return false;
const lang = m[3] || "en";
const customSlug = (typeof frontmatter.slug === "string");
const urlSlug = m[2];// + (lang == "es" ? "-es": "");
const slug = (customSlug) ? frontmatter.slug : urlSlug;
let meta = {
lang, urlSlug, customSlug, slug,
type: m[1],
template: path.resolve(`./src/templates/default.js`),
};
if(meta.type == "lesson") meta.template = path.resolve(`./src/templates/lesson.js`);
return meta;
};
exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions;
const tagTemplate = path.resolve("src/templates/tags.js");
return new Promise((resolve, reject) => {
graphql(`
{
allMarkdownRemark {
edges {
node {
fileAbsolutePath
fields {
lang
slug
url
}
frontmatter{
tags
slug
}
}
}
}
}
`).then(result => {
const translations = buildTranslations(result.data.allMarkdownRemark.edges);
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
const meta = getMetaFromPath(node);
if(meta){
if(meta.lang === "en" || meta.customSlug){
createPage({
path: node.fields.url,
component: meta.template,
context: {
// Data passed to context is available in page queries as GraphQL variables.
url: node.fields.url,
slug: node.fields.slug,
lang: meta.lang,
type: meta.type,
urlSlug: meta.urlSlug,
translations: translations[meta.urlSlug]
},
});
}
createPage({
path: meta.lang+"/"+node.fields.url,
component: meta.template,
context: {
// Data passed to context is available in page queries as GraphQL variables.
url: meta.lang+"/"+node.fields.url,
slug: node.fields.slug,
lang: meta.lang,
type: meta.type,
urlSlug: meta.urlSlug,
translations: translations[meta.urlSlug]
},
});
}
});
const posts = result.data.allMarkdownRemark.edges;
// Tag pages:
let tags = [];
// Iterate through each post, putting all found tags into `tags`
_.each(posts, edge => {
if (_.get(edge, "node.frontmatter.tags")) {
tags = tags.concat(edge.node.frontmatter.tags);
}
});
// Eliminate duplicate tags
tags = _.uniq(tags);
// Make tag pages
tags.forEach(tag => {
createPage({
path: `/tags/${_.kebabCase(tag)}/`,
component: tagTemplate,
context: {
tag,
},
});
});
resolve();
}).catch(err => console.error(err));
});
};