gatsby#GatsbyNode TypeScript Examples
The following examples show how to use
gatsby#GatsbyNode.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: create-pages.ts From nyxo-website with MIT License | 6 votes |
createPages: GatsbyNode["createPages"] = async ({
graphql,
actions,
}) => {
const { createPage } = actions
const allMarkdown: {
errors?: string
data?: {
allBlogPosts?: {
nodes?: Post[]
}
}
} = await graphql(markdownQuery)
if (allMarkdown.errors) {
throw allMarkdown.errors
}
const posts = allMarkdown?.data?.allBlogPosts?.nodes
posts?.forEach((post: Post, index: number) => {
const previous = index === posts?.length - 1 ? null : posts[index + 1].slug
const next = index === 0 ? null : posts[index - 1].slug
createPage({
path: post.fields.slug as string,
component: path.resolve(`./src/templates/blog-post.tsx`),
context: {
next,
previous,
author: post.frontmatter.authorSlug,
slug: post.fields.slug,
},
})
})
}
Example #2
Source File: onCreateNode.ts From usehooks-ts with MIT License | 6 votes |
onCreateNode: GatsbyNode['onCreateNode'] = args => {
const { node, actions } = args
if (node.internal.type === 'Mdx') {
const absolutePath = node.fileAbsolutePath as string
const isHookRegex = new RegExp('^use[A-Z][a-zA-Z]*$')
const file = absolutePath.split('/').reverse()[0].split('.')
const filename = file[0]
const type = file.length === 3 ? file[1] : 'post'
if (isHookRegex.test(filename)) {
actions.createNodeField({
node,
name: `name`,
value: filename,
})
actions.createNodeField({
node,
name: `type`,
value: type,
})
actions.createNodeField({
node,
name: 'path',
value: `/${camelToKebabCase(filename)}`,
})
}
}
}
Example #3
Source File: gatsby-node.ts From blog.ojisan.io with MIT License | 6 votes |
createPages: GatsbyNode["createPages"] = async ({
actions,
graphql,
}) => {
const { createPage } = actions;
const tagTemplate = path.resolve(`./src/templates/tags-template.tsx`);
const tagsResult = await graphql(`
{
tags: allMarkdownRemark {
group(field: frontmatter___tags) {
tag: fieldValue
totalCount
}
}
}
`);
if (tagsResult.data === undefined) throw new Error("invalid query");
// eslint-disable-next-line
// @ts-ignore
tagsResult.data.tags.group.forEach((data) => {
createPage({
path: `/tags/${toLower(data.tag)}/`,
component: tagTemplate,
context: {
tag: data.tag,
},
});
});
}
Example #4
Source File: gatsby-node.ts From gatsby-remark-related-posts with MIT License | 6 votes |
createSchemaCustomization: GatsbyNode['createSchemaCustomization'] =
({ actions }, user_option) => {
const option: Option = {
...default_option,
...user_option,
};
actions.createTypes(`
type related${option.target_node}s implements Node {
posts: [${option.target_node}]
}
`);
}
Example #5
Source File: gatsby-node.ts From defund12.org with MIT License | 5 votes |
createPages: GatsbyNode["createPages"] = async ({
actions,
graphql,
reporter,
}) => {
const { createPage, createRedirect } = actions;
/** Shared logic between creating email and letter pages */
async function createEmailOrLetterTemplatePage({
layout,
component,
}: {
layout: string;
component: string;
}) {
const templateData = await graphql(`
{
allMarkdownRemark(filter: { frontmatter: { layout: { eq: "${layout}" } } }) {
edges {
node {
frontmatter {
permalink
redirect_from
}
}
}
}
}
`);
if (templateData.errors) {
reporter.panicOnBuild("Error while querying for email data.");
return;
}
(templateData.data as TemplateQueryData).allMarkdownRemark.edges.forEach(
({ node }) => {
// Create individual pages
createPage({
path: node.frontmatter.permalink,
component: component,
context: {
permalink: node.frontmatter.permalink,
},
});
// Create redirects
node.frontmatter.redirect_from?.forEach((redirectFrom) => {
createRedirect({
fromPath: redirectFrom,
toPath: node.frontmatter.permalink,
isPermanent: true,
});
});
}
);
}
await createEmailOrLetterTemplatePage({
layout: "email",
component: require.resolve("./src/components/email/Email.tsx"),
});
await createEmailOrLetterTemplatePage({
layout: "letter",
component: require.resolve("./src/components/letter/Letter.tsx"),
});
}
Example #6
Source File: createPages.ts From usehooks-ts with MIT License | 5 votes |
createPages: GatsbyNode['createPages'] = async args => {
await Promise.all([createHooks(args)])
}
Example #7
Source File: gatsby-node.ts From gatsby-remark-related-posts with MIT License | 4 votes |
onPostBootstrap: GatsbyNode['onPostBootstrap'] = async (
{ actions, getNode, getNodesByType, createNodeId, reporter },
user_option
) => {
const option: Option = {
...default_option,
...user_option,
};
const nodes = getNodesByType(option.target_node);
// add documents to tfidf
const docs = nodes.map((node) => ({
id: node.id,
text: option.getMarkdown(node),
}));
const tfidf = new TfIdf();
for (let doc of docs) {
tfidf.addDocument(
await getSpaceSeparatedDoc[option.doc_lang](getTextFromMarkdown(doc.text))
);
}
// generate bow vectors
type Term = TfIdfTerm & {
tf: number;
idf: number;
};
//// extract keywords from each document
const doc_terms = docs.map((_, i) =>
(tfidf.listTerms(i) as Term[])
.map((x) => ({ ...x, tfidf: (x as Term).tf * (x as Term).idf }))
.sort((x, y) => y.tfidf - x.tfidf)
);
// DEBUG: print terms
// doc_terms.forEach((x, i) =>
// console.log(
// docs[i].id,
// x.map((x) => x.term)
// )
//);
const all_keywords = new Set<string>();
const tfidf_map_for_each_doc: Map<string, number>[] = [];
doc_terms.forEach((x, i) => {
tfidf_map_for_each_doc[i] = new Map<string, number>();
x.slice(0, option.each_bow_size).forEach((x) => {
all_keywords.add(x.term);
tfidf_map_for_each_doc[i].set(x.term, x.tfidf);
});
});
//// generate vectors
const bow_vectors = new Map<string, BowVector>();
docs.forEach((x, i) => {
if (bow_vectors === null) return;
bow_vectors.set(
x.id,
Array.from(all_keywords)
.map((x) => tfidf_map_for_each_doc[i].get(x))
.map((x) => (x === undefined ? 0 : x))
);
});
reporter.info(
`[related-posts] bow vectors generated, dimention: ${all_keywords.size}`
);
// create related nodes
nodes.forEach((node) => {
const related_nodes = getRelatedPosts(node.id, bow_vectors)
.slice(1)
.map((id) => getNode(id));
const digest = `${node.id} >>> related${option.target_node}s`;
actions.createNode({
id: createNodeId(digest),
parent: node.id,
internal: {
type: `related${option.target_node}s`,
contentDigest: digest,
},
posts: related_nodes,
});
});
}