Skip to content

Commit

Permalink
First stab
Browse files Browse the repository at this point in the history
  • Loading branch information
ptgott committed Oct 8, 2024
1 parent 2843b63 commit 41377ac
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions server/remark-includes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,6 @@ 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 @@ -167,13 +165,28 @@ export function parseParamDefaults(expr: string): ParameterAssignments {
return {};
}

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

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

return assignments;
}

// resolveParamValue takes the quoted value of a template variable within a
Expand Down

0 comments on commit 41377ac

Please sign in to comment.