diff --git a/README.md b/README.md index 8844651..cd08284 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/index.d.ts b/lib/index.d.ts index 1deb063..e41d3b3 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -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; /** diff --git a/src/index.js b/src/index.js index 3f15d62..e0ddc94 100644 --- a/src/index.js +++ b/src/index.js @@ -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. @@ -78,7 +78,8 @@ const defaultOptions = { trailingSlash: false, linksets: [], duplicates: 'error', - directoryIndex: 'index.html' + directoryIndex: 'index.html', + pattern: ':dirname/:basename' } /** @@ -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 = /\||:|<|>|\*|\?|"/ @@ -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, './') }