Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
bgoonz committed Aug 16, 2022
1 parent cb4346e commit 04b3a3e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
24 changes: 21 additions & 3 deletions plugins/gatsby-remark-page-creator/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,44 @@ const fs = require("fs");
const {createFilePath} = require("gatsby-source-filesystem");
const _ = require("lodash");


function findFileNode({node, getNode}) {
let fileNode = node;
let ids = [fileNode.id];

while (fileNode && fileNode.internal.type !== `File` && fileNode.parent) {
fileNode = getNode(fileNode.parent);

if (!fileNode) {
break;
}

if (_.includes(ids, fileNode.id)) {
console.log(`found cyclic reference between nodes`);
break;
}

ids.push(fileNode.id);
}

if (!fileNode || fileNode.internal.type !== `File`) {
console.log('did not find ancestor File node');
return null;
}
<<<<<<< HEAD
=======

>>>>>>> master
return fileNode
}

exports.onCreateNode = ({node, getNode, actions}, options) => {

const {createNodeField} = actions;

if (node.internal.type === "MarkdownRemark") {
let fileNode = findFileNode({node, getNode});
if (!fileNode) {
throw new Error('could not find parent File node for MarkdownRemark node: ' + node);
}

let url;
if (node.frontmatter.url) {
url = node.frontmatter.url;
Expand All @@ -42,6 +49,7 @@ exports.onCreateNode = ({node, getNode, actions}, options) => {
} else {
url = createFilePath({node, getNode});
}

createNodeField({node, name: "url", value: url});
createNodeField({node, name: "absolutePath", value: fileNode.absolutePath});
createNodeField({node, name: "relativePath", value: fileNode.relativePath});
Expand All @@ -52,8 +60,10 @@ exports.onCreateNode = ({node, getNode, actions}, options) => {
createNodeField({node, name: "name", value: fileNode.name});
}
};

exports.createPages = ({graphql, getNode, actions, getNodesByType}) => {
const {createPage, deletePage} = actions;

// Use GraphQL to bring only the "id" and "html" (added by gatsby-transformer-remark)
// properties of the MarkdownRemark nodes. Don't bring additional fields
// such as "relativePath". Otherwise, Gatsby's GraphQL resolvers might infer
Expand All @@ -75,12 +85,14 @@ exports.createPages = ({graphql, getNode, actions, getNodesByType}) => {
if (result.errors) {
return Promise.reject(result.errors);
}

const nodes = result.data.allMarkdownRemark.edges.map(({node}) => node);
const siteNode = getNode('Site');
const siteDataNode = getNode('SiteData');
const sitePageNodes = getNodesByType('SitePage');
const sitePageNodesByPath = _.keyBy(sitePageNodes, 'path');
const siteData = _.get(siteDataNode, 'data', {});

const pages = nodes.map(graphQLNode => {
// Use the node id to get the underlying node. It is not exactly the
// same node returned by GraphQL, because GraphQL resolvers might
Expand All @@ -96,6 +108,7 @@ exports.createPages = ({graphql, getNode, actions, getNodesByType}) => {
html: graphQLNode.html
};
});

nodes.forEach(graphQLNode => {
const node = getNode(graphQLNode.id);
const url = node.fields.url;
Expand All @@ -105,12 +118,15 @@ exports.createPages = ({graphql, getNode, actions, getNodesByType}) => {
console.error(`Error: undefined template for ${url}`);
return;
}

const component = path.resolve(`./src/templates/${template}.js`);
if (!fs.existsSync(component)) {
console.error(`Error: component "src/templates/${template}.js" missing for ${url}`);
return;
}

const existingPageNode = _.get(sitePageNodesByPath, url);

const page = {
path: url,
component: component,
Expand All @@ -130,9 +146,11 @@ exports.createPages = ({graphql, getNode, actions, getNodesByType}) => {
}
}
};

if (existingPageNode && !_.get(page, 'context.menus')) {
page.context.menus = _.get(existingPageNode, 'context.menus');
}

createPage(page);
});
});
Expand Down
18 changes: 17 additions & 1 deletion plugins/gatsby-source-data/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,35 @@ const fse = require('fs-extra');
const chokidar = require('chokidar');
const _ = require('lodash');

const metadataFileName = `site-metadata.json`;

const metadataFileName = 'site-metadata.json';

const parsers = {
yaml: (data) => yaml.safeLoad(data, {schema: yaml.JSON_SCHEMA}),
json: (data) => JSON.parse(data)
};

const supportedExtensions = {
'yaml': parsers.yaml,
'yml': parsers.yaml,
'json': parsers.json
};

exports.sourceNodes = (props, pluginOptions = {}) => {
const createContentDigest = props.createContentDigest;
const { createNode } = props.actions;
const reporter = props.reporter;

if (!_.get(pluginOptions, 'path')) {
pluginOptions.path = 'src/data';
}

if (!path.isAbsolute(pluginOptions.path)) {
pluginOptions.path = path.resolve(process.cwd(), pluginOptions.path)
}

reporter.info(`[gatsby-source-data] setup file watcher and create site data`);

const dataPath = pluginOptions.path;
const createSiteDataFromFilesPartial = _.partial(createSiteDataFromFiles, { dataPath, createNode, createContentDigest, reporter });
const watcher = chokidar.watch([dataPath, metadataFileName], {
Expand All @@ -34,22 +42,28 @@ exports.sourceNodes = (props, pluginOptions = {}) => {
watcher.on('add', createSiteDataFromFilesPartial);
watcher.on('change', createSiteDataFromFilesPartial);
watcher.on('unlink', createSiteDataFromFilesPartial);

return createSiteDataFromFiles({ dataPath, createNode, createContentDigest, reporter }, null);
};

async function createSiteDataFromFiles({ dataPath, createNode, createContentDigest, reporter }, changedFile) {
reporter.info(`[gatsby-source-data] create site data from files, updated path: ${changedFile}`);
let dataFiles = [];

const dataPathExists = await fse.pathExists(dataPath);
if (dataPathExists) {
dataFiles = await readDirRecursively(dataPath);
}

const metadataPath = path.resolve(metadataFileName);
const metadataExists = await fse.pathExists(metadataPath);
if (metadataExists) {
dataFiles.push(metadataFileName);
}

const sortedDataFiles = dataFiles.slice().sort();
const data = await convertDataFilesToJSON(sortedDataFiles, dataPath, reporter);

createNode({
id: 'SiteData',
parent: null,
Expand All @@ -62,6 +76,7 @@ async function createSiteDataFromFiles({ dataPath, createNode, createContentDige
}
});
}

async function readDirRecursively(dir, options) {
const rootDir = _.get(options, 'rootDir', dir);
const files = await fse.readdir(dir);
Expand All @@ -79,6 +94,7 @@ async function readDirRecursively(dir, options) {
const recFiles = await Promise.all(promises);
return _.chain(recFiles).compact().flatten().value();
}

function convertDataFilesToJSON(dataFiles, dataDirPath, reporter) {
let promises = _.map(dataFiles, filePath => {
const pathObject = path.parse(filePath);
Expand Down

1 comment on commit 04b3a3e

@vercel
Copy link

@vercel vercel bot commented on 04b3a3e Aug 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deployment failed with the following error:

Resource is limited - try again in 17 hours (more than 100, code: "api-deployments-free-per-day").

Please sign in to comment.