Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

fix: fix the problem that some pages cannot be found #207

Merged
merged 1 commit into from
Mar 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 31 additions & 34 deletions docs/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
*/
const path = require('path');
const { createFilePath } = require('gatsby-source-filesystem');
const { snakeCase } = require('lodash')
const { snakeCase } = require('lodash');

exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
resolve: {
modules: [path.resolve(__dirname, 'node_modules')],
alias: {
'wizard-ui': path.resolve(__dirname, '../src'),
}
}
})
}
},
},
});
};

exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const { createPage } = actions;

const componentPage = path.resolve(`./src/templates/components-post.js`);
const defaultPage = path.resolve(`./src/templates/default.js`);
Expand All @@ -28,7 +28,7 @@ exports.createPages = async ({ graphql, actions }) => {
`
{
allMdx(
sort: { fields: [frontmatter___date], order: DESC },
sort: { fields: [frontmatter___date], order: DESC }
filter: { frontmatter: { title: { ne: "" } } }
limit: 1000
) {
Expand Down Expand Up @@ -71,11 +71,11 @@ exports.createPages = async ({ graphql, actions }) => {
`
);
if (result.errors) {
throw result.errors
throw result.errors;
}

// Create blog posts pages.
const posts = result.data.allMdx.edges
const posts = result.data.allMdx.edges;
const propTables = result.data.allComponentMetadata.edges;

posts.forEach((post, index) => {
Expand All @@ -88,56 +88,53 @@ exports.createPages = async ({ graphql, actions }) => {
context: {
slug,
},
})
});
} catch (e) {
console.log(e);
}
} else {
const next = index === posts.length - 1 ? null : posts[index + 1].node
const previous = index === 0 ? null : posts[index - 1].node
const next = index === posts.length - 1 ? null : posts[index + 1].node;
const previous = index === 0 ? null : posts[index - 1].node;
// 组件 propTypes 解析
const propTableDatas = propTables.filter(pt => {
const pSlug = pt.node.fields.slug;
const path = `/components/${snakeCase(pSlug).replace(/_/g, '-')}/`;
return slug === path;
});
const propDatas = propTableDatas.length > 0 ? propTableDatas[0] : null;
if(propDatas) {
try {
createPage({
path: slug,
component: componentPage,
context: {
slug,
previous,
next,
propDatas,
},
})
} catch (e) {
console.log(e);
}
try {
createPage({
path: slug,
component: componentPage,
context: {
slug,
previous,
next,
propDatas,
},
});
} catch (e) {
console.log(e);
}
}
})
});

return null;

}
};

exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
const { createNodeField } = actions;

if ([`Mdx`, 'ComponentMetadata'].includes(node.internal.type)) {
const value = createFilePath({ node, getNode })
const value = createFilePath({ node, getNode });
try {
createNodeField({
name: `slug`,
node,
value,
})
});
} catch (e) {
console.log(e);
}
}
}
};