Skip to content

Commit

Permalink
Formalize default permalink pattern to ':dirname/:basename'
Browse files Browse the repository at this point in the history
  • Loading branch information
webketje committed Oct 5, 2023
1 parent 058c61e commit 176515e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ Metalsmith(__dirname).use(
)
```
The `pattern` can contain a reference to any piece of metadata associated with the file by using the `:PROPERTY` syntax for placeholders.
The `pattern` can contain a reference to **any piece of metadata associated with the file** by using the `:PROPERTY` syntax for placeholders.
By default, all files get a `:dirname/:basename` (+ directoryIndex = `/index.html`) pattern, i.e. the original filepath `blog/post1.html` becomes `blog/post1/index.html`. The `dirname` and `basename` values are automatically made available by @metalsmith/permalinks for the purpose of generating the permalink.
If no pattern is provided, the files won't be remapped, but the `permalink` metadata key will still be set, so that you can use it for outputting links to files in the template.
Expand Down
3 changes: 2 additions & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export type Linkset = {
*/
export type Options = {
/**
* A permalink pattern to transform file paths into, e.g. `blog/:date/:title`
* A permalink pattern to transform file paths into, e.g. `blog/:date/:title`.
* @default ':dirname/:basename'
*/
pattern?: string;
/**
Expand Down
19 changes: 15 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const dupeHandlers = {
* `@metalsmith/permalinks` options & default linkset
*
* @typedef {Object} Options
* @property {string} [pattern] A permalink pattern to transform file paths into, e.g. `blog/:date/:title`
* @property {string} [pattern=':dirname/:basename'] A permalink pattern to transform file paths into, e.g. `blog/:date/:title`. Default is `:dirname/:basename`.
* @property {string} [date='YYYY/MM/DD'] [Moment.js format string](https://momentjs.com/docs/#/displaying/format/) to transform Date link parts into, defaults to `YYYY/MM/DD`.
* @property {string} [directoryIndex='index.html'] Basename of the permalinked file (default: `index.html`)
* @property {boolean} [trailingSlash=false] Whether a trailing `/` should be added to the `file.permalink` property. Useful to avoid redirects on servers which do not have a built-in rewrite module enabled.
Expand All @@ -78,7 +78,8 @@ const defaultOptions = {
trailingSlash: false,
linksets: [],
duplicates: 'error',
directoryIndex: 'index.html'
directoryIndex: 'index.html',
pattern: ':dirname/:basename'
}

/**
Expand Down Expand Up @@ -254,7 +255,17 @@ function permalinks(options) {
: normalizedOptions.slug,
date: typeof linkset.date === 'string' ? format(linkset.date) : normalizedOptions.date
}
let ppath = replace(linkset.pattern, data, opts) || resolve(file, normalizedOptions.directoryIndex)
let ppath =
replace(
linkset.pattern,
{
...data,
basename:
path.basename(file) === normalizedOptions.directoryIndex ? '' : path.basename(file, path.extname(file)),
dirname: path.dirname(file)
},
opts
) || resolve(file, normalizedOptions.directoryIndex)

// invalid on Windows, but best practice not to use them anyway
const invalidFilepathChars = /\||:|<|>|\*|\?|"/
Expand All @@ -275,7 +286,7 @@ function permalinks(options) {
}

// add to permalink data for use in links in templates
let permalink = ppath === '.' ? '' : ppath.replace(/\\/g, '/')
let permalink = path.posix.join('.', ppath.replace(/\\/g, '/'))
if (normalizedOptions.trailingSlash) {
permalink = path.posix.join(permalink, './')
}
Expand Down

0 comments on commit 176515e

Please sign in to comment.