Skip to content

Commit

Permalink
Component manifests - Escape input strings
Browse files Browse the repository at this point in the history
  • Loading branch information
pattra committed Aug 30, 2024
1 parent 0a7563f commit 768e663
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const createActions = async ({
typeInterface: createTypeInterface(action.key ?? actionKey),
import: createImport(action.key ?? actionKey),
key: action.key || actionKey,
label: action.display.description,
label: action.display.label,
description: action.display.description,
inputs,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { escapeSpecialCharacters } from "../utils/escapeSpecialCharacters";
import { ServerTypeInput } from "./getInputs";

export const DOC_BLOCK_DEFAULT = (input: ServerTypeInput): string => {
Expand Down Expand Up @@ -34,9 +35,11 @@ export const addLine = ({ key, value, raw }: AddLineProps) => {
return "";
}

const sanitizedValue = JSON.stringify(value)
.replace(/(^"|"$)|(^'|'$)/g, "")
.trim();
const sanitizedValue = escapeSpecialCharacters(
JSON.stringify(value)
.replace(/(^"|"$)|(^'|'$)/g, "")
.trim(),
);

return ` * ${key ? `@${key} ${sanitizedValue}` : sanitizedValue}\n`;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Input as InputBase } from "../../serverTypes";
import type { InputFieldDefinition } from "../../types/Inputs";
import { escapeSpecialCharacters } from "../utils/escapeSpecialCharacters";
import { DOC_BLOCK_DEFAULT } from "./docBlock";

export type ServerTypeInput = InputBase & {
Expand All @@ -25,11 +26,12 @@ interface GetInputsProps {
}

const getDefaultValue = (value: ServerTypeInput["default"]) => {
if (value === undefined || value === "" || typeof value === "string") {
if (value === undefined || value === "") {
return value;
}

return JSON.stringify(value);
const stringValue = typeof value === "string" ? value : JSON.stringify(value);
return escapeSpecialCharacters(stringValue);
};

export const getInputs = ({ inputs, docBlock = DOC_BLOCK_DEFAULT }: GetInputsProps): Input[] => {
Expand Down
12 changes: 12 additions & 0 deletions packages/spectral/src/generators/utils/escapeSpecialCharacters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* This regex targets common characters that may be included in default
* input values (code comment blocks, backticks, etc) and would cause
* component-manifest build issues. More characters may be added
* as discovered.
*/

const escapeRegEx = /(\/|\\|\`|\$)/g;

export const escapeSpecialCharacters = (value = ""): string => {
return value.replace(escapeRegEx, "\\$&");
};

0 comments on commit 768e663

Please sign in to comment.