Skip to content

Commit

Permalink
feat(plugin-bibtex): add option to force use of label
Browse files Browse the repository at this point in the history
  • Loading branch information
larsgw committed May 7, 2024
1 parent a92f6b3 commit d5631e2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
2 changes: 2 additions & 0 deletions packages/plugin-bibtex/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ const config = plugins.config.get('@bibtex')
| `config.parse.strict` | `true`, [`false`] | When true, entries are checked for required fields. |
| `config.parse.sentenceCase` | `'always'`, `'english'`, [`'never'`] | Convert titles to sentence case when parsing. |
| `config.format.useIdAsLabel` | `true`, [`false`] | Use the entry ID as the label instead of generating one. |
| `config.format.checkLabel` | [`true`], `false` | Remove unsafe characters from the provided label (or ID). |
| `config.format.asciiOnly` | [`true`], `false` | Escape or remove non-ASCII characters. |

### Type mappings

Expand Down
2 changes: 2 additions & 0 deletions packages/plugin-bibtex/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,12 @@ export default {
* @memberof module:@citation-js/plugin-bibtex.config
* @var {Object} format
* @property {Boolean} [format.useIdAsLabel=false] - Use the entry ID as the label instead of generating one.
* @property {Boolean} [format.checkLabel=true] - Remove unsafe characters from the provided label (or ID).
* @property {Boolean} [format.asciiOnly=true] - Escape or remove non-ASCII characters.
*/
format: {
useIdAsLabel: false,
checkLabel: true,
asciiOnly: true
},

Expand Down
34 changes: 20 additions & 14 deletions packages/plugin-bibtex/src/mapping/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ const stopWords = new Set([
const unsafeChars = /(?:<\/?.*?>|[\u0020-\u002F\u003A-\u0040\u005B-\u005E\u0060\u007B-\u007F])+/g
const unicode = /[^\u0020-\u007F]+/g

function isLabelSafe (text) {
return !config.format.checkLabel || !text.match(unsafeChars)
}

function formatLabelFromId (id) {
if (id === null) {
return 'null'
} else if (id === undefined) {
return 'undefined'
} else if (config.format.checkLabel) {
return id.toString().replace(unsafeChars, '')
} else {
return id.toString()
}
}

function firstWord (text) {
if (!text) {
return ''
Expand Down Expand Up @@ -287,22 +303,12 @@ export const Converters = {
return [label, label]
},
toSource (id, label, author, issued, suffix, title) {
let safeId
if (id === null) {
safeId = 'null'
} else if (id === undefined) {
safeId = 'undefined'
} else {
safeId = id.toString().replace(unsafeChars, '')
}
if (config.format.useIdAsLabel) {
return safeId
}

if (label && !unsafeChars.test(label)) {
if (label && isLabelSafe(label)) {
return label
} else if (config.format.useIdAsLabel) {
return formatLabelFromId(id)
} else {
return formatLabel(author, issued, suffix, title) || safeId
return formatLabel(author, issued, suffix, title) || formatLabelFromId(id)
}
}
},
Expand Down

0 comments on commit d5631e2

Please sign in to comment.