Skip to content

Commit

Permalink
Don't encode verbatim fields (eg url) (#415)
Browse files Browse the repository at this point in the history
  • Loading branch information
FlamingTempura committed Sep 7, 2024
1 parent 9ad5c22 commit eb389af
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 22 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,5 @@ Providing multiple input files will only work in `--modify`/`-m` mode.

- Support filenames with spaces (#416, #428)
- Do not flatten braces which contain commands (#407, #423)
- Upgrade dependencies
- Do not encode special characters in verbatim fields like url (#415)
- Upgrade dependencies
12 changes: 11 additions & 1 deletion bibtex-tidy.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion bin/bibtex-tidy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/bundle.js

Large diffs are not rendered by default.

20 changes: 18 additions & 2 deletions src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ import {
wrapText,
} from "./utils";

/**
* The following fields are listed in the BibLaTeX documentation as verbatim (may contain
* special characters). Source: Kime et al (2024) The biblatex Package (v3.20).
*/
const VERBATIM_FIELDS = [
"url",
"doi",
"eprint",
"file",
"verba",
"verbb",
"verbc",
"pdf",
];

export function formatBibtex(
ast: RootNode,
options: OptionsNormalized,
Expand Down Expand Up @@ -207,8 +222,9 @@ export function formatValue(
if (nameLowerCase === "url" && encodeUrls) {
value = escapeURL(value);
}
// escape special characters like %
if (enableEscape) {
// escape special characters like %. Do not do this on the url field, which is a
// special bibtex field where special characters are output verbatim.
if (!VERBATIM_FIELDS.includes(nameLowerCase) && enableEscape) {
value = escapeSpecialCharacters(value);
}
if (nameLowerCase === "pages") {
Expand Down
29 changes: 13 additions & 16 deletions test/encode-urls.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,26 @@ import { strictEqual } from "node:assert";
import { bibtex, bibtexTidy, test } from "./utils";

const input = bibtex`
@inproceedings{Smith2009,
author="Caroline JA Smith",
year=2009,
month=dec,
title={{Quantum somethings}},journal={Journal of {B}lah},
booktitle={JOURNAL OF SOMETHINGS},
url={http://example.com/something_with/unusual?characters=faoo#bar}
}`;

const output = bibtex`
const expectedUnencoded = bibtex`
@inproceedings{Smith2009,
url = {http://example.com/something_with/unusual?characters=faoo#bar}
}
`;

const expectedEncoded = bibtex`
@inproceedings{Smith2009,
author = "Caroline JA Smith",
year = 2009,
month = dec,
title = {{Quantum somethings}},
journal = {Journal of {B}lah},
booktitle = {JOURNAL OF SOMETHINGS},
url = {http://example.com/something\%5Fwith/unusual?characters=faoo\#bar}
url = {http://example.com/something\%5Fwith/unusual?characters=faoo#bar}
}
`;

test("encode urls", async () => {
const tidied = await bibtexTidy(input, { encodeUrls: true });
strictEqual(tidied.bibtex, output);
const output1 = await bibtexTidy(input, { encodeUrls: false });
strictEqual(output1.bibtex, expectedUnencoded);

const output2 = await bibtexTidy(input, { encodeUrls: true });
strictEqual(output2.bibtex, expectedEncoded);
});

0 comments on commit eb389af

Please sign in to comment.