Skip to content

Commit

Permalink
Deprecates options.indexFile in favor of options.directoryIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
webketje committed Jan 3, 2023
1 parent 4130e03 commit b6ee083
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
8 changes: 6 additions & 2 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,17 @@ export type Options = {
*/
date?: string;
/**
* When `true` (by default), will duplicate sibling files so relative links keep working in resulting structure. Turn off by setting `false`. Can also be set to `folder`, which uses a strategy that considers files in folder as siblings if the folder is named after the html file.
* _**[DEPRECATED]** - _will be defaulted to false and removed in the next major version_. When `true` (by default), will duplicate sibling files so relative links keep working in resulting structure. Turn off by setting `false`. Can also be set to `folder`, which uses a strategy that considers files in folder as siblings if the folder is named after the html file.
*/
relative?: boolean | 'folder';
/**
* Basename of the permalinked file (default: `index.html`)
* **[DEPRECATED]** - _renamed to directoryIndex_. Basename of the permalinked file (default: `index.html`)
*/
indexFile?: string;
/**
* Basename of the permalinked file (default: `index.html`)
*/
directoryIndex?: string;
/**
* **[DEPRECATED]** - _use `duplicates` option instead_. Set to `true` to add a number to duplicate permalinks (default: `false`), or specify a custom duplicate handling callback of the form `(permalink, files, file, options) => string`
*/
Expand Down
25 changes: 16 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import * as route from 'regexparam'

const dupeHandlers = {
error(targetPath, filesObj, filename, opts) {
const target = path.join(targetPath, opts.indexFile)
const target = path.join(targetPath, opts.directoryIndex)
const currentBaseName = path.basename(filename)

if (filesObj[target] && currentBaseName !== opts.indexFile) {
if (filesObj[target] && currentBaseName !== opts.directoryIndex) {
return new Error(`Permalinks: Clash with another target file ${target}`)
}
return target
Expand All @@ -18,13 +18,13 @@ const dupeHandlers = {
counter = 0,
postfix = ''
do {
target = path.join(`${targetPath}${postfix}`, opts.indexFile)
target = path.join(`${targetPath}${postfix}`, opts.directoryIndex)
postfix = `-${++counter}`
} while (filesObj[target])
return target
},
overwrite(targetPath, filesObj, filename, opts) {
return path.join(targetPath, opts.indexFile)
return path.join(targetPath, opts.directoryIndex)
}
}

Expand Down Expand Up @@ -64,8 +64,9 @@ const dupeHandlers = {
* @typedef {Object} Options
* @property {string} [pattern] A permalink pattern to transform file paths into, e.g. `blog/:date/:title`
* @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 {boolean|'folder'} [relative=true] _**[DEPRECATED]** - _will be altered or removed in the next major version_. When `true` (by default), will duplicate sibling files so relative links keep working in resulting structure. Turn off by setting `false`. Can also be set to `folder`, which uses a strategy that considers files in folder as siblings if the folder is named after the html file.
* @property {string} [indexFile='index.html'] Basename of the permalinked file (default: `index.html`)
* @property {boolean|'folder'} [relative=true] _**[DEPRECATED]** - _will be defaulted to false and removed in the next major version_. When `true` (by default), will duplicate sibling files so relative links keep working in resulting structure. Turn off by setting `false`. Can also be set to `folder`, which uses a strategy that considers files in folder as siblings if the folder is named after the html file.
* @property {string} [indexFile='index.html'] _**[DEPRECATED]** - _renamed to directoryIndex_. Basename of the permalinked file (default: `index.html`)
* @property {string} [directoryIndex='index.html'] Basename of the permalinked file (default: `index.html`)
* @property {boolean|Function} [unique] **[DEPRECATED]** - _use `duplicates` option instead_. Set to `true` to add a number to duplicate permalinks (default: `false`), or specify a custom duplicate handling callback of the form `(permalink, files, file, options) => string`
* @property {boolean} [duplicatesFail=false] **[DEPRECATED]** - _use `duplicates` option instead_. Set to `true` to throw an error if multiple file path transforms result in the same permalink. `false` by default
* @property {'error'|'index'|'overwrite'|Function} [duplicates] How to handle duplicate target URI's.
Expand Down Expand Up @@ -179,6 +180,12 @@ const normalizeOptions = (options) => {
}
} else if (Object.keys(dupeHandlers).includes(options.duplicates)) {
options.duplicates = dupeHandlers[options.duplicates]
} else {
options.duplicates = dupeHandlers.overwrite
}

if (options.indexFile && !options.directoryIndex) {
options.directoryIndex = options.indexFile
}

return options
Expand Down Expand Up @@ -247,10 +254,10 @@ const folder = (file, files) => {
* @param {String} str The path
* @return {String}
*/
const resolve = (str, indexFile = 'index.html') => {
const resolve = (str, directoryIndex = 'index.html') => {
const base = path.basename(str, path.extname(str))
let ret = path.dirname(str)
if (base !== path.basename(indexFile, path.extname(indexFile))) {
if (base !== path.basename(directoryIndex, path.extname(directoryIndex))) {
ret = path.join(ret, base).replace(/\\/g, '/')
}

Expand Down Expand Up @@ -361,7 +368,7 @@ function permalinks(options) {

debug('applying pattern: %s to file: %s', linkset.pattern, file)

let ppath = replace(linkset.pattern, data, linkset) || resolve(file, options.indexFile)
let ppath = replace(linkset.pattern, data, linkset) || resolve(file, options.directoryIndex)

let fam
switch (linkset.relative) {
Expand Down

0 comments on commit b6ee083

Please sign in to comment.