From b8e9ff2ead27445c64021f4b85d01bea158bafbc Mon Sep 17 00:00:00 2001 From: Arctic Ice Studio Date: Sun, 18 Nov 2018 20:48:11 +0100 Subject: [PATCH] Implement base constants and Gatsby configuration with basic plugins All metadata and constants, like defined in GH-26, are placed in the corresponding folders for configurations (`src/config`) and data (`src/data`). Initially this includes information stored in the `package.json` file of the project and the `nord` package which has also been installed. This commit also adds base and important constants that will be used later on: - `src/config/internal/constants.js` - Provides internally used constants - `src/config/internal/nodes.js` - Provides internally used data about custom Gatsby GraphQL API nodes. - `src/config/routes/constants.js` - Provides routing constants. - `src/config/routes/mappings.js` - Provides route mapping constants. GH-27 --- gatsby-config.js | 81 ++++++++++++++++++++++++++++ package-lock.json | 33 +++++------- package.json | 3 +- src/config/internal/constants.js | 90 ++++++++++++++++++++++++++++++++ src/config/internal/nodes.js | 80 ++++++++++++++++++++++++++++ src/config/routes/constants.js | 76 +++++++++++++++++++++++++++ src/config/routes/mappings.js | 57 ++++++++++++++++++++ src/data/project.js | 62 ++++++++++++++++++++++ 8 files changed, 460 insertions(+), 22 deletions(-) create mode 100644 gatsby-config.js create mode 100644 src/config/internal/constants.js create mode 100644 src/config/internal/nodes.js create mode 100644 src/config/routes/constants.js create mode 100644 src/config/routes/mappings.js create mode 100644 src/data/project.js diff --git a/gatsby-config.js b/gatsby-config.js new file mode 100644 index 00000000..0f6ec1e6 --- /dev/null +++ b/gatsby-config.js @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2018-present Arctic Ice Studio + * Copyright (C) 2018-present Sven Greb + * + * Project: Nord Docs + * Repository: https://github.com/arcticicestudio/nord-docs + * License: MIT + */ + +/** + * @file The Gatsby configuration. + * @author Arctic Ice Studio + * @author Sven Greb + * @see https://gatsbyjs.org/docs/gatsby-config + * @since 0.1.0 + */ + +const { metadataNord, metadataNordDocs } = require("./src/data/project"); +const { sourceInstanceTypes } = require("./src/config/internal/nodes"); +const { + BASE_DIR_CONTENT, + BASE_DIR_ASSETS_IMAGES, + BASE_DIR_CONFIG, + BASE_DIR_PAGES +} = require("./src/config/internal/constants"); +const { BASE_PUBLIC_URL } = require("./src/config/routes/constants"); + +module.exports = { + siteMetadata: { + nord: { ...metadataNord }, + ...metadataNordDocs + }, + plugins: [ + "gatsby-plugin-react-helmet", + "gatsby-plugin-catch-links", + "gatsby-plugin-remove-trailing-slashes", + "gatsby-plugin-no-sourcemaps", + "gatsby-transformer-yaml", + { + resolve: "gatsby-plugin-canonical-urls", + options: { + siteUrl: `${BASE_PUBLIC_URL}` + } + }, + { + resolve: "gatsby-source-filesystem", + options: { + name: "images", + path: `${__dirname}/${BASE_DIR_ASSETS_IMAGES}` + } + }, + { + resolve: "gatsby-source-filesystem", + options: { + name: `${sourceInstanceTypes.blog.id}`, + path: `${__dirname}/${BASE_DIR_CONTENT}/${sourceInstanceTypes.blog.path}/` + } + }, + { + resolve: "gatsby-source-filesystem", + options: { + name: "config", + path: `${__dirname}/${BASE_DIR_CONFIG}/` + } + }, + { + resolve: "gatsby-source-filesystem", + options: { + name: `${sourceInstanceTypes.docs.id}`, + path: `${__dirname}/${BASE_DIR_CONTENT}/${sourceInstanceTypes.docs.path}/` + } + }, + { + resolve: "gatsby-source-filesystem", + options: { + name: "pages", + path: `${__dirname}/${BASE_DIR_PAGES}/` + } + } + ] +}; diff --git a/package-lock.json b/package-lock.json index 976fe696..b0441834 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5294,13 +5294,11 @@ }, "balanced-match": { "version": "1.0.0", - "bundled": true, - "optional": true + "bundled": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, - "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -5313,18 +5311,15 @@ }, "code-point-at": { "version": "1.1.0", - "bundled": true, - "optional": true + "bundled": true }, "concat-map": { "version": "0.0.1", - "bundled": true, - "optional": true + "bundled": true }, "console-control-strings": { "version": "1.1.0", - "bundled": true, - "optional": true + "bundled": true }, "core-util-is": { "version": "1.0.2", @@ -5427,8 +5422,7 @@ }, "inherits": { "version": "2.0.3", - "bundled": true, - "optional": true + "bundled": true }, "ini": { "version": "1.3.5", @@ -5438,7 +5432,6 @@ "is-fullwidth-code-point": { "version": "1.0.0", "bundled": true, - "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -5451,20 +5444,17 @@ "minimatch": { "version": "3.0.4", "bundled": true, - "optional": true, "requires": { "brace-expansion": "^1.1.7" } }, "minimist": { "version": "0.0.8", - "bundled": true, - "optional": true + "bundled": true }, "minipass": { "version": "2.2.4", "bundled": true, - "optional": true, "requires": { "safe-buffer": "^5.1.1", "yallist": "^3.0.0" @@ -5481,7 +5471,6 @@ "mkdirp": { "version": "0.5.1", "bundled": true, - "optional": true, "requires": { "minimist": "0.0.8" } @@ -5554,8 +5543,7 @@ }, "number-is-nan": { "version": "1.0.1", - "bundled": true, - "optional": true + "bundled": true }, "object-assign": { "version": "4.1.1", @@ -5565,7 +5553,6 @@ "once": { "version": "1.4.0", "bundled": true, - "optional": true, "requires": { "wrappy": "1" } @@ -5671,7 +5658,6 @@ "string-width": { "version": "1.0.2", "bundled": true, - "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -9339,6 +9325,11 @@ } } }, + "nord": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/nord/-/nord-0.2.1.tgz", + "integrity": "sha1-BE7Y4AMxyEqW3ExnDwgKclcsPPk=" + }, "normalize-package-data": { "version": "2.4.0", "resolved": "https://registry.npmjs.org/normalize-package-data/-/normalize-package-data-2.4.0.tgz", diff --git a/package.json b/package.json index 82e885ba..73d9768d 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,7 @@ "gatsby-plugin-react-helmet": "3.0.2", "gatsby-plugin-remove-trailing-slashes": "2.0.5", "gatsby-source-filesystem": "2.0.8", - "gatsby-transformer-yaml": "2.1.5" + "gatsby-transformer-yaml": "2.1.5", + "nord": ">=0.2.1 <1.0.0" } } diff --git a/src/config/internal/constants.js b/src/config/internal/constants.js new file mode 100644 index 00000000..fc275e32 --- /dev/null +++ b/src/config/internal/constants.js @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2018-present Arctic Ice Studio + * Copyright (C) 2018-present Sven Greb + * + * Project: Nord Docs + * Repository: https://github.com/arcticicestudio/nord-docs + * License: MIT + */ + +/** + * @file Provides internally used constants. + * @author Arctic Ice Studio + * @author Sven Greb + * @since 0.1.0 + */ + +/** + * The absolute path of the content base directory starting from the project root. + * + * @constant {String} + * @since 0.1.0 + */ +const BASE_DIR_CONTENT = "content"; + +/** + * The absolute path of the sources base directory starting from the project root. + * + * @constant {string} + * @since 0.1.0 + */ +const BASE_DIR_SRC = "src"; + +/** + * The absolute path of the assets base directory starting from the project root. + * + * @constant {string} + * @since 0.1.0 + */ +const BASE_DIR_ASSETS = `${BASE_DIR_SRC}/assets`; + +/** + * The absolute path of the assets directory for images starting from the project root. + * + * @constant {string} + * @since 0.1.0 + */ +const BASE_DIR_ASSETS_IMAGES = `${BASE_DIR_ASSETS}/images`; + +/** + * The absolute path of the config base directory starting from the project root. + * + * @constant {string} + * @since 0.1.0 + */ +const BASE_DIR_CONFIG = `${BASE_DIR_SRC}/config`; + +/** + * The absolute path of the pages base directory starting from the project root. + * + * @constant {string} + * @since 0.1.0 + */ +const BASE_DIR_PAGES = `${BASE_DIR_SRC}/pages`; + +/** + * The internal type for MDX nodes. + * + * @constant {String} + * @since 0.1.0 + * @see https://github.com/mdx-js/mdx + */ +const NODE_TYPE_MDX = "Mdx"; + +/** + * Regular expression to match the date of a blog post from the file path. + * + * @constant {RegExp} + * @since 0.1.0 + */ +const REGEX_BLOG_POST_DATE = /([0-9]+)\/([0-9]+)\/([0-9]+)\/(.+)/; + +module.exports = { + BASE_DIR_ASSETS, + BASE_DIR_ASSETS_IMAGES, + BASE_DIR_CONFIG, + BASE_DIR_CONTENT, + BASE_DIR_PAGES, + NODE_TYPE_MDX, + REGEX_BLOG_POST_DATE +}; diff --git a/src/config/internal/nodes.js b/src/config/internal/nodes.js new file mode 100644 index 00000000..9bb498f4 --- /dev/null +++ b/src/config/internal/nodes.js @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2018-present Arctic Ice Studio + * Copyright (C) 2018-present Sven Greb + * + * Project: Nord Docs + * Repository: https://github.com/arcticicestudio/nord-docs + * License: MIT + */ + +/** + * @file Provides internally used data about custom Gatsby GraphQL API nodes. + * @author Arctic Ice Studio + * @author Sven Greb + * @since 0.1.0 + */ + +/** + * The names of the source instance types and their paths relative to the project root path. + * + * @type {Object} + * @since 0.1.0 + */ +const sourceInstanceTypes = { + blog: { id: "blog", path: "blog" }, + docs: { id: "docs", path: "docs" } +}; + +/** + * The additional fields added to nodes with the Gatsby Node `onCreateNode` API. + * + * @type {Object} + * @see https://gatsbyjs.org/docs/node-apis + * @see https://next.gatsbyjs.org/docs/node-apis/#onCreateNode + * @since 0.1.0 + */ +const nodeFields = { + /** + * The date a blog post has been created. + */ + date: { + name: "date", + type: "string", + targetContentSourceTypes: [sourceInstanceTypes.blog.id] + }, + /** + * The content type of the node based on the source instance of the configured `gatsby-source-filesystem` Gatsby + * plugin. + */ + contentSourceType: { + name: "contentSourceType", + type: "string", + targetContentSourceTypes: [...Object.keys(sourceInstanceTypes).map(name => sourceInstanceTypes[name].id)] + }, + /** + * The slug of the node without the prepended parent route. + */ + slug: { + name: "slug", + type: "string", + targetContentSourceTypes: [...Object.keys(sourceInstanceTypes).map(name => sourceInstanceTypes[name].id)] + }, + /** + * The relative directory of the node. + */ + relativeDirectory: { + name: "relativeDirectory", + type: "string", + targetContentSourceTypes: [...Object.keys(sourceInstanceTypes).map(name => sourceInstanceTypes[name].id)] + }, + /** + * The slug of the parent route based on the content source type. + */ + slugParentRoute: { + name: "slugParentRoute", + type: "string", + targetContentSourceTypes: [...Object.keys(sourceInstanceTypes).map(name => sourceInstanceTypes[name].id)] + } +}; + +module.exports = { nodeFields, sourceInstanceTypes }; diff --git a/src/config/routes/constants.js b/src/config/routes/constants.js new file mode 100644 index 00000000..6953bf10 --- /dev/null +++ b/src/config/routes/constants.js @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2018-present Arctic Ice Studio + * Copyright (C) 2018-present Sven Greb + * + * Project: Nord Docs + * Repository: https://github.com/arcticicestudio/nord-docs + * License: MIT + */ + +/** + * @file Provides routing constants. + * @author Arctic Ice Studio + * @author Sven Greb + * @since 0.1.0 + */ + +const { metadataNord } = require("../../data/project"); + +/** + * The default path separator. + * + * @constant {string} + * @since 0.1.0 + */ +const pathSeparator = "/"; + +/** + * The public base URL of the site. + * The value can be injected through the {@linkcode NORD_DOCS_BASE_PUBLIC_URL} environment + * variable during compile time, otherwise defaults to the specified homepage of the project. + * + * @constant {string} + * @since 0.1.0 + */ +const BASE_PUBLIC_URL = process.env.NORD_DOCS_BASE_PUBLIC_URL || `${metadataNord.homepage}`; + +/** + * The root route name. + * + * @constant {string} + * @since 0.1.0 + */ +const ROOT = pathSeparator; + +/** + * The route name of the "blog" page. + * + * @constant {string} + * @since 0.1.0 + */ +const BLOG = "blog"; + +/** + * The route name of the "docs" page. + * + * @constant {string} + * @since 0.1.0 + */ +const DOCS = "docs"; + +/** + * The route name of the landing page. + * Alias that will be redirected to the {@link ROOT} route. + * + * @constant {string} + * @since 0.1.0 + */ +const LANDING = "landing"; + +module.exports = { + BASE_PUBLIC_URL, + BLOG, + DOCS, + LANDING, + ROOT +}; diff --git a/src/config/routes/mappings.js b/src/config/routes/mappings.js new file mode 100644 index 00000000..f2e2aecb --- /dev/null +++ b/src/config/routes/mappings.js @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2018-present Arctic Ice Studio + * Copyright (C) 2018-present Sven Greb + * + * Project: Nord Docs + * Repository: https://github.com/arcticicestudio/nord-docs + * License: MIT + */ + +/** + * @file Provides route mapping constants. + * @author Arctic Ice Studio + * @author Sven Greb + * @since 0.1.0 + */ + +const { ROOT, BLOG, DOCS, LANDING } = require("./constants"); + +/** + * The root route mapping. + * + * @constant {string} + * @since 0.1.0 + */ +const ROUTE_ROOT = ROOT; + +/** + * The route mapping for the "blog" page. + * + * @constant {string} + * @since 0.1.0 + */ +const ROUTE_BLOG = ROUTE_ROOT + BLOG; + +/** + * The route mapping for the "docs" page. + * + * @constant {string} + * @since 0.1.0 + */ +const ROUTE_DOCS = ROUTE_ROOT + DOCS; + +/** + * The route mapping for the landing page. + * Alias that will be redirected to {@link ROUTE_ROOT}. + * + * @constant {string} + * @since 0.1.0 + */ +const ROUTE_LANDING = ROUTE_ROOT + LANDING; + +module.exports = { + ROUTE_ROOT, + ROUTE_BLOG, + ROUTE_DOCS, + ROUTE_LANDING +}; diff --git a/src/data/project.js b/src/data/project.js new file mode 100644 index 00000000..83aef257 --- /dev/null +++ b/src/data/project.js @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2018-present Arctic Ice Studio + * Copyright (C) 2018-present Sven Greb + * + * Project: Nord Docs + * Repository: https://github.com/arcticicestudio/nord-docs + * License: MIT + */ + +/** + * @file Provides metadata about the `nord` and `nord-docs` packages. + * @author Arctic Ice Studio + * @author Sven Greb + * @see https://github.com/arcticicestudio/nord + * @since 0.1.0 + */ + +const { author, description, homepage, name, repository, title, version } = require("nord/package.json"); + +const { + author: pkgAuthor, + description: pkgDescription, + homepage: pkgHomepage, + name: pkgName, + repository: pkgRepository, + title: pkgTitle, + version: pkgVersion +} = require("../../package.json"); + +/** + * Provides metadata about this `nord-docs` project package. + * + * @type {Object} + * @since 0.1.0 + */ +const metadataNord = { + author, + description, + homepage: pkgHomepage || homepage, + name, + repository, + title, + version +}; + +/** + * Provides metadata about the `nord-docs` package. + * + * @type {Object} + * @since 0.1.0 + */ +const metadataNordDocs = { + author: pkgAuthor, + description: pkgDescription, + homepage: pkgHomepage || homepage, + name: pkgName, + repository: pkgRepository, + title: pkgTitle, + version: pkgVersion +}; + +module.exports = { metadataNord, metadataNordDocs };