Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow processing of custom markdown nodes #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ module.exports = {
// [Optional] Exclude the following fields, use dot notation for nested fields
// No fields are excluded by default
exclude: ['featured.skip'],
// [Optional] in case you want to process markdown nodes that were not created from a file
// you can provide a resolver function which produces the absolute path for a given node.
// In the example below the remarkNode was a child node of a fileNode. E.g. if you have
// markdown content in your frontmatter fields.
resolveNodePath: ({ node, getNode }) => {
let fileAbsolutePath = undefined;
let curNode = node;
while (curNode.parent && !fileAbsolutePath) {
curNode = getNode(curNode.parent);
fileAbsolutePath =
curNode.fileAbsolutePath || curNode.absolutePath;
if (fileAbsolutePath) {
return fileAbsolutePath;
}
}
return undefined;
},
},
},
{
Expand Down
23 changes: 20 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { selectAll } from 'unist-util-select';
import { defaults, isString, find } from 'lodash';
import cheerio from 'cheerio';
import { slash } from './utils';
import { GatsbyNode } from 'gatsby';

export type GatsbyNodePluginArgs = {
files: GatsbyFile[];
Expand All @@ -11,16 +12,24 @@ export type GatsbyNodePluginArgs = {
reporter: {
info: (msg: string, error?: Error) => void;
};
getNode: (id: string) => GatsbyNode;
};

export type GatsbyFile = {
absolutePath: string;
};

export interface ResolveNodeArgs {
node: GatsbyNode;
getNode: (id: string) => GatsbyNode;
}
export type ResolveNodeFunc = (args: ResolveNodeArgs) => string;

export type PluginOptions = {
staticFolderName: string;
include: string[];
exclude: string[];
resolveNodePath?: ResolveNodeFunc;
};

export type FrontMatterOptions = {
Expand Down Expand Up @@ -72,15 +81,23 @@ export const findMatchingFile = (
};

export default async (
{ files, markdownNode, markdownAST }: GatsbyNodePluginArgs,
{ files, markdownNode, markdownAST, getNode }: GatsbyNodePluginArgs,
pluginOptions: PluginOptions
) => {
// Default options
const options = defaults(pluginOptions, defaultPluginOptions);

if (!markdownNode.fileAbsolutePath) return;
let fileAbsolutePath = markdownNode.fileAbsolutePath;

if (!fileAbsolutePath && pluginOptions.resolveNodePath) {
fileAbsolutePath = pluginOptions.resolveNodePath({
node: markdownNode,
getNode,
});
}
if (!fileAbsolutePath) return;

const directory = path.dirname(markdownNode.fileAbsolutePath);
const directory = path.dirname(fileAbsolutePath);

// Process all markdown image nodes
selectAll('image', markdownAST).forEach((_node: any) => {
Expand Down
9 changes: 7 additions & 2 deletions src/on-create-node.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { GatsbyNode } from 'gatsby';
import path from 'path';
import { defaults, isString } from 'lodash';
import traverse from 'traverse';
Expand All @@ -12,21 +13,25 @@ import {
export type GatsbyPluginArgs = {
node: MarkdownNode;
getNodesByType: (type: string) => GatsbyFile[];
getNode: (id: string) => GatsbyNode;
reporter: {
info: (msg: string, error?: Error) => void;
};
};

export const onCreateNode = (
{ node, getNodesByType }: GatsbyPluginArgs,
{ node, getNodesByType, getNode }: GatsbyPluginArgs,
pluginOptions: PluginOptions
) => {
const options = defaults(pluginOptions, defaultPluginOptions);

if (node.internal.type === `MarkdownRemark` || node.internal.type === `Mdx`) {
const files = getNodesByType(`File`);

const directory = path.dirname(node.fileAbsolutePath);
let fileAbsolutePath = node.fileAbsolutePath;
if (!fileAbsolutePath && pluginOptions.resolveNodePath) {
}
const directory = path.dirname(fileAbsolutePath);

// Deeply iterate through frontmatter data for absolute paths
traverse(node.frontmatter).forEach(function (value) {
Expand Down