Skip to content

Commit

Permalink
Resolves #122: remove deprecated substitute pkg, add regexparam instead
Browse files Browse the repository at this point in the history
  • Loading branch information
webketje committed Jan 30, 2022
1 parent 4684767 commit 6f225b3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8,495 deletions.
24 changes: 7 additions & 17 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const path = require('path');
const debug = require('debug')('@metalsmith/permalinks');
const moment = require('moment');
const slugify = require('slugify');
const substitute = require('substitute');
const route = require('regexparam');

const error = debug.extend('error');

Expand Down Expand Up @@ -210,7 +210,8 @@ const resolve = str => {
*/
const replace = (pattern, data, options) => {
if (!pattern) return null;
const keys = params(pattern);
const parsed = route.parse(pattern);
const keys = parsed.keys
const ret = {};

for (let i = 0, key; (key = keys[i++]); ) {
Expand All @@ -223,21 +224,10 @@ const replace = (pattern, data, options) => {
}
}

return substitute(pattern, ret);
};

/**
* Get the params from a `pattern` string.
*
* @param {string} pattern
* @return {Array}
*/
const params = pattern => {
const matcher = /:(\w+)/g;
const ret = [];
let m;
while ((m = matcher.exec(pattern))) ret.push(m[1]);
return ret;
const transformed = route.inject(pattern, ret)
// handle absolute paths
if (transformed.startsWith('/')) return transformed.slice(1)
return transformed
};

/**
Expand Down
Loading

0 comments on commit 6f225b3

Please sign in to comment.