Skip to content

Commit

Permalink
Allow HTML in help messages
Browse files Browse the repository at this point in the history
  • Loading branch information
BalaM314 committed Dec 22, 2024
1 parent de35b69 commit ee783d2
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions core/src/utils/funcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ export type ConfigSuggestion<T> = {
config: Config<T, boolean>;
value: T extends number ? "increase" | "decrease" : T;
};
export type HTMLMessage = {
/** This text will be inserted as HTML into the error message when Soodocode is being run in a web browser. */
html: string;
/** This text will be displayed if Soodocode is not being run in a web browser. */
text: string;
}

export type RichErrorMessage = {
/** Short summary of the error, displayed at the top */
Expand All @@ -192,7 +198,7 @@ export type RichErrorMessage = {
/**
* Suggests a solution to the error
*/
help?: string | ErrorMessageLine[] | ConfigSuggestion<any>;
help?: string | (ErrorMessageLine | HTMLMessage)[] | ConfigSuggestion<any>;
};

export type ErrorMessage = string | RichErrorMessage;
Expand Down Expand Up @@ -231,9 +237,12 @@ export class SoodocodeError extends Error {
}
const { help } = this.richMessage;
if(help){
if(Array.isArray(help) || typeof help === "string"){
//Lines or string
output += array(help).map(line => `help: ${formatErrorLine(line, sourceCode)}`).join("\n");
if(typeof help === "string"){
return `help: ${help}`;
} else if(Array.isArray(help)){
output += help.map(line =>
`help: ${typeof line != "string" && "text" in line ? line.text : formatErrorLine(line, sourceCode)}`
).join("\n");
} else {
output += `help: ${help.message ?? `To allow this`}, ${
help.value === true ? `enable the config "${help.config.name}"` :
Expand All @@ -257,17 +266,23 @@ export class SoodocodeError extends Error {
let output = "";
output += span(formatErrorLine(this.richMessage.summary, sourceCode), "error-message") + "\n";
if(this.richMessage.elaboration){
output += array(this.richMessage.elaboration).map(line => " &bull; " + span(formatErrorLine(line, sourceCode), "error-message-elaboration") + "\n").join("");
output += array(this.richMessage.elaboration).map(line =>
" &bull; " + span(formatErrorLine(line, sourceCode), "error-message-elaboration") + "\n"
).join("");
}
if(this.richMessage.context){
output += array(this.richMessage.context).map(line => "\t" + span(formatErrorLine(line, sourceCode), "error-message-context") + "\n").join("");
output += array(this.richMessage.context).map(line =>
"\t" + span(formatErrorLine(line, sourceCode), "error-message-context") + "\n"
).join("");
}
const { help } = this.richMessage;
if(help){
output += "\n"; //Extra blank line before the help message
if(Array.isArray(help) || typeof help === "string"){
//Lines or string
output += span(array(help).map(line => `help: ${formatErrorLine(line, sourceCode)}`).join("\n"), "error-message-help");
output += `<span class="error-message-help">${array(help).map(line =>
`help: ${typeof line != "string" && "html" in line ? line.html : formatErrorLine(line, sourceCode)}`
).join("\n")}</span>`;
} else {
const shouldCreateButton = typeof help.value !== "string";
//Add a button that enables it
Expand Down

0 comments on commit ee783d2

Please sign in to comment.