Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
Revert default-setting logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ptgott committed Oct 8, 2024
1 parent cca48d8 commit 5d015bf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 52 deletions.
33 changes: 10 additions & 23 deletions server/remark-includes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ export function parsePartialParams(expr: string): ParameterAssignments {
// The default assignments expression must be on the first line of a partial.
// Values must be wrapped in double quotes and separated by single spaces.
export function parseParamDefaults(expr: string): ParameterAssignments {
const defaultAssignmentRegexp = new RegExp("{{ (.*) }}", "g");

// Callers should handle empty values before they get here.
// parseParamDefaults has no idea whether this case is acceptable or not.
if (expr === "") {
Expand All @@ -165,40 +167,25 @@ export function parseParamDefaults(expr: string): ParameterAssignments {
return {};
}

const possibleAssignmentRegexp = new RegExp("{{ (.*) }}", "g");
const paramUseRegexp = new RegExp("{{ (\\w+) }}", "g");
const firstLine = expr.split("\n", 1)[0];
const firstLineMatches = possibleAssignmentRegexp.exec(firstLine);
const paramUses = Array.from(expr.matchAll(paramUseRegexp));
const matches = defaultAssignmentRegexp.exec(firstLine);

let assignments: ParameterAssignments = {};
// Treat this as an attempted default parameter assignment
if (!!firstLineMatches && firstLineMatches[1].includes("=")) {
assignments = parseAssignments(firstLineMatches[1]);
}
if (paramUses.length == 0) {
return assignments;
if (!matches) {
return {};
}
// The default value for parameters without a default is the empty string.
paramUses.forEach((use) => {
if (!assignments[use[1]]) {
assignments[use[1]] = "";
}
});

return assignments;
return parseAssignments(matches[1]);
}

// resolveParamValue takes the quoted value of a template variable within a
// partial and returns the unquoted value, resolving any escapes. Escaped
// quotations are resolved within double-quoted strings but not single-quoted
// strings.
//
// Throws an exception if the quotes are mismatched or the input is not wrapped
// in quotes.
// Throws an exception if the quotes are mismatched, the input is not wrapped
// in quotes, or the input is empty
export function resolveParamValue(val: string): string {
if (val === "") {
return "";
if (val == "") {
throw new Error("the value of a parameter in a partial cannot be empty");
}
if (val[0] !== `"`) {
throw new Error(
Expand Down
30 changes: 1 addition & 29 deletions uvu-tests/remark-includes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,35 +407,7 @@ This is a partial.
description: "short partial with no defaults and one param",
shouldThrow: false,
input: `This is partial with a {{ param }}`,
expected: {
param: "",
},
},
{
description: "partial with multiple params and no defaults",
shouldThrow: false,
input: `This is a partial with a {{ first }}.
It also includes the {{ second }} param.`,
expected: {
first: "",
second: "",
},
},
{
description: "partial with three params and two defaults",
shouldThrow: false,
input: `{{ first="1" second="2" }}
This is a partial with a {{ first }}.
It also includes the {{ second }} param.
Finally, there is {{ third }}.
`,
expected: {
first: `"1"`,
second: `"2"`,
third: "",
},
expected: {},
},
{
description: "double curly braces and an equals",
Expand Down

0 comments on commit 5d015bf

Please sign in to comment.