doc.silverstripe.org/gatsby-node.js

166 lines
4.5 KiB
JavaScript
Raw Normal View History

2019-11-08 03:40:20 +01:00
const path = require('path');
2019-12-13 04:48:27 +01:00
const fs = require('fs');
2019-11-08 03:40:20 +01:00
const { createFilePath } = require(`gatsby-source-filesystem`);
const fileToTitle = require('./src/utils/fileToTitle');
const createSlug = ({path, version, thirdparty}) => {
const rest = path.split('/');
const parts = [
'en',
version,
// thirdparty modules are explicitly pathed
thirdparty,
...rest,
].filter(p => p);
const slug = parts
.map(part => part.replace(/^\d+_/, ''))
.join('/')
.toLowerCase();
return `/${slug}/`;
2019-11-08 03:40:20 +01:00
};
const parseName = name => name.split('--');
exports.onCreateNode = async ({ node, getNode, getNodesByType, actions, createNodeId, createContentDigest }) => {
2019-11-08 03:40:20 +01:00
if (node.internal.type !== 'MarkdownRemark') {
return;
}
const { createNode } = actions;
const fileNode = getNode(node.parent);
const [category, version, thirdparty] = parseName(fileNode.sourceInstanceName);
2019-11-19 01:55:33 +01:00
// The gatsby-sgatsbource-filesystem plugins are registered to collect from the same path
2019-11-19 01:55:33 +01:00
// that the git source writes to, so we get the watch task (hot reload on content changes)
// But we don't want duplicate document pages for each source plugin, so
// we bail out if we already have the file. However, we need to ensure
// the file is injected into the template as a dependency, so when the content changes,
// the pages get refreshed on the fly.
if (category === 'watcher') {
2019-11-19 01:55:33 +01:00
const existing = getNodesByType('SilverstripeDocument')
.find(n => n.fileAbsolutePath === node.fileAbsolutePath);
2019-11-19 01:55:33 +01:00
if (existing) {
// Pair the document with its watched file so we can inject it into the template
// as a dependency.
existing.watchFile___NODE = node.id;
}
return;
}
2024-02-18 21:26:23 +01:00
const basePath = category === 'user' ? 'docs/en/userguide' : (thirdparty ? 'docs/en' : 'en');
2019-11-08 03:40:20 +01:00
const filePath = createFilePath({
node,
getNode,
basePath,
2019-11-19 01:55:33 +01:00
});
2019-11-08 03:40:20 +01:00
let fileTitle = path.basename(node.fileAbsolutePath, '.md');
const isIndex = fileTitle === 'index';
if (isIndex) {
fileTitle = path.basename(path.dirname(node.fileAbsolutePath));
}
const docTitle = fileToTitle(fileTitle);
const slug = createSlug({
path: filePath,
version,
thirdparty,
});
2019-11-08 03:40:20 +01:00
const parentSlug = `${path.resolve(slug, '../')}/`;
const unhideSelf = false;
2019-11-19 01:55:33 +01:00
// Most of these don't exist in userhelp, so force them into the schema by un-nulling them.
const frontmatter = {
introduction: ``,
icon: `file-alt`,
iconBrand: ``,
hideChildren: false,
...node.frontmatter,
};
2019-11-08 03:40:20 +01:00
const docData = {
isIndex,
filePath,
fileTitle,
slug,
parentSlug,
unhideSelf,
category,
...frontmatter,
2019-11-08 03:40:20 +01:00
};
if (!docData.title || docData.title === '') {
docData.title = docTitle;
}
const docInternal = {
type: `SilverstripeDocument`,
2019-11-18 05:51:57 +01:00
contentDigest: createContentDigest({
...docData,
rawMarkdownBody: node.rawMarkdownBody,
}),
2019-11-08 03:40:20 +01:00
};
const nodeData = {
2019-11-08 03:40:20 +01:00
...node,
2019-11-19 01:55:33 +01:00
id: createNodeId(`SilverstripeDocument${node.id}`),
2019-11-08 03:40:20 +01:00
...docData,
parent: node.id,
internal: docInternal,
2019-11-19 01:55:33 +01:00
}
2019-11-08 03:40:20 +01:00
createNode(nodeData);
};
2019-12-16 01:53:53 +01:00
exports.createPages = async ({ actions, graphql, getNodesByType }) => {
const { createPage, createRedirect } = actions;
2019-11-08 03:40:20 +01:00
const docTemplate = path.resolve(`src/templates/docs-template.tsx`);
const result = await graphql(`
{
allSilverstripeDocument {
nodes {
2019-11-18 05:51:57 +01:00
id
2019-11-08 03:40:20 +01:00
slug
}
}
}`);
2019-11-08 03:40:20 +01:00
if (result.errors) {
throw new Error(result.errors);
}
result.data.allSilverstripeDocument.nodes
.forEach(node => {
createPage({
path: node.slug,
component: docTemplate,
context: {
2019-11-18 05:51:57 +01:00
id: node.id,
2019-11-08 03:40:20 +01:00
slug: node.slug,
}
});
})
2019-11-08 03:40:20 +01:00
2019-12-13 04:48:27 +01:00
2019-12-16 01:53:53 +01:00
console.log(`Creating legacy redirects...`);
const redirects = new Map();
2023-05-05 07:07:47 +02:00
const v5docs = getNodesByType('SilverstripeDocument').filter(n => n.slug.match(/^\/en\/5\//));
2019-12-16 01:53:53 +01:00
const v4docs = getNodesByType('SilverstripeDocument').filter(n => n.slug.match(/^\/en\/4\//));
const v3docs = getNodesByType('SilverstripeDocument').filter(n => n.slug.match(/^\/en\/3\//));
2023-05-05 07:07:47 +02:00
[...v5docs, ...v4docs, ...v3docs].forEach(n => {
2019-12-16 01:53:53 +01:00
const legacy = n.slug.replace(/^\/en\/[0-9]\//, '/en/');
2023-05-05 07:07:47 +02:00
if (legacy === '/en/') {
return;
}
2019-12-16 01:53:53 +01:00
if (!redirects.has(legacy)) {
redirects.set(legacy, n.slug);
}
});
redirects.forEach((toPath, fromPath) => {
createRedirect({ fromPath, toPath, isPermanent: true });
});
2019-12-13 04:48:27 +01:00
2019-12-16 01:53:53 +01:00
};