orgajs icon indicating copy to clipboard operation
orgajs copied to clipboard

Custom URL for pathnames doesn't work after last upgrade

Open kepi opened this issue 7 years ago • 3 comments

Hello,

after upgrade to latest version, I'm unable to use custom URL (as shown in https://github.com/xiaoxinghu/gatsby-orga/blob/master/gatsby-node.js where code is commented now).

My Org pages are stored in src/help and I want them accessible on https://siteurl/help/<title>/. Problem is that after migration from orga to OrgContent it looks like like slugs are created directly in plugin (owner of slug field is orga transform plugin) and I'm no longer able to add slug field through onCreateNode.

IMHO this should work:

const { createFilePath } = require(`gatsby-source-filesystem`)

// Add custom url pathname for blog posts.
exports.onCreateNode = ({ node, getNode, actions }) => {
  const { createNodeField } = actions

  if (node.internal.type === `OrgContent`) {
    const path = createFilePath({ node, getNode })
    const slug = `/help${path}`

    createNodeField({
      node,
      name: `slug`,
      value: slug,
    })
  }
}

But I'm getting error Error: A plugin tried to update a node field that it doesn't own

Is there any chance to get old behaviour back or is there correst way how to proceed? I tried many approaches so far without success.

kepi avatar Feb 06 '19 18:02 kepi

Hi, thanks for the feedback. Yes I changed that behaviour because orga-transformer is a little bit more complicated than the remark-transformer (markdown plugin for gatsby) when it comes to dealing with slug. Markdown files are 1 to 1 mapped to the final pages. But orga support 1 to many mapping with #+ORGA_PUBLISH_KEYWORD in buffer setting. Basically you can pick and choose subtrees to publish into standalone pages. In order to unify the slug logic, there are 2 concept: #+EXPORT_FILE_NAME and #+CATEGORY, so the slug eventually will be [CATEGORY]/[EXPORT_FILE_NAME]. With the current implementation, you can't change that logic, because it's in the plugin. The purpose is to take away the complexity from users. But it's still flexible enough to achieve your goal. You can simply add this in the beginning of your org files:

#+CATEGORY: help

If you want customised file name too, add #+EXPORT_FILE_NAME: my-file-name, then the plugin will pick that up. Without that option, the plugin will use the real file name, this is useful when you want to keep your permalink stable, but you have to change your file names. In theory, you can even be a little bit hacky by doing this: #+EXPORT_FILE_NAME: help/my-file-name.

Please let me know if it doesn't work as expected. Thanks!

xiaoxinghu avatar Feb 06 '19 22:02 xiaoxinghu

Thanks for quick answer, I thought that new "category wise splitting" might be the reason.

I think your proposed solution will work, but seams really like anti-pattern in our case. We want to include our Org documents as part of Gatsby site on specific prefix and adding category and export_file_name to every Org file seams ugly.

Anyway, I'm not sure what would be best approach to allow behavior we want. Specially because I still didn't get whole logic behind gatsby transformer plugins :)

First two ideas comming to my mind:

  • add something like slug-prefix (or something like this) for gatsby-transformer-orga in gatsby-config
  • generate some "default" slug in orga transformer and still allow to change it through onCreateNode

Second approach is more appealing to me as I would be able to generate whole slugs, not only prefix them. As we are structuring our org files in sub-directories, it is little redundant to add #+CATEGORY too. It would also allow us to add more logic to slugs generation. Problem is I'm not sure how this should work, I tought that source plugin is creating default slug which is used when there is no field.slug present, but maybe it isn't the case.

Let me know what you think. It would be great if you could add support for this but I'm also looking for feedback in case you don't have time but you will be willing to merge PR.

Thanks

kepi avatar Feb 07 '19 08:02 kepi

@kepi You are right. The do-it-all-for-you design maybe a good thing in a consumer facing product, but it is not suitable for a power tool, not to mention a software plugin. I removed the login from the plugin itself, now I'm sticking to however the markdown plugin is doing. Plus, Gatsby now introduced the concept of theme, maybe that's a better place to do opinionated things. Take a look at the www folder for the latest example.

xiaoxinghu avatar Aug 27 '19 22:08 xiaoxinghu