metalsmith / permalinks

A Metalsmith plugin for permalinks.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allow Processing Before Metalsmith-In-Place

jcrawford opened this issue · comments

It appears that metalsmith-permalinks must be run after metalsmith-in-place as it requires the use of an HTML file

if (!html(file)) return;

I have a plugin that will process the markdown of my files and create indexes in Algolia for searching, this is working perfectly breaking each document up into chunks, however there is one gotcha that I have been hitting.

I have to provide a URL for my Algolia objects so that the search component knows where to link to. There could also be anchor links in my markdown which are processed so I end up with something like this.

[
  {
    "objectID":"articles/getting-started-with-metalsmith/first-page-42329a17ccc7b8eb80eb42d1d558092d",
    "h1":"Starting wtih Metalsmith",
    "link":"articles/getting-started-with-metalsmith/first-page",
    "importance":0
  },
  {
    "objectID":"articles/getting-started-with-metalsmith/first-page#before-we-begin-ae5e3efcdddc387616bbc5b1e5b1b134",
    "h1":"Starting wtih Metalsmith",
    "h3":"Before we Begin",
    "link":"articles/getting-started-with-metalsmith/first-page#before-we-begin",
    "content":"Before we get started I need to make the disclaimer that I am not a Metalsmith expert. The experiences outlined here are experiences I have had along the road to migrating to Metalsmith. If you see something that is incorrect or if you know a better way to do something please speak up and let me know. I am providing this walk-through to get people started with migrating to Metalsmith.",
    "importance":5
  }
]

As you can see there is a link attribute that is sent to Algolia with the json document(s).

Since permalinks cannot be run before metalsmith-in-place I cannot run it prior to my plugin which means I cannot get access to what the final path will be after permalinks is run.

I am trying to make this plugin modular so that anyone could use it and not simply for my project.

Is there any way that I can get at what the permalink would be or would it be tough to allow this to process before metalsmith-in-place? I was going to process the HTML but processing the markdown turned out far easier. I even explored converting the html to markdown and then processing it that way but that was even more tough to process.

A PR allowing the ability to provide a pattern would resolve this issue.

I am not quite sure what you mean.

A file_pattern to match. E.g.:

permalinks({
   file_pattern: '*.html'
 })

@woodyrew @jcrawford
I have submitted a PR with documentation to solve this. #90 Includes docs and tests passed.

added rename option, defaults to true to maintain previous functionality.

.use(permalinks({ rename: false }))
.use(inPlace())

Until this is looked at I'm using my own plugin for my specific use case if anyone else needs it.

@woodyrew is this still open for submissions? Happy to push out a quick PR that enables file pattern matching, as well as the option to preserve original extensions.

What about instead of adding a rename option, adding an ext or extname option?

  • ext === '.html' (default) rename extensions to .html
  • ext === false do not touch file extensions
  • ext === <any> , where any is any file extension (e.g. .json/.yml/.csv/etc)

This would also enable e.g. creating static JSON API's from yml definition files.

Hmm this is a tough issue. pattern is a confusingly named option as all other plugins use it for targeting existing file paths and not file paths to be. I wholeheartedly agree to the need of being able to target files other than .html, but that would require the introduction of a pattern option that defaults to **/*.html and conflicts with the current pattern option.

The current pattern option would better be renamed to permalink and the currently outputted path property (which conflicts with metalsmith-paths plugin) would better be renamed to permalink too.