Skip to content

Commit

Permalink
Handle spaces better in Markdown parse.ts.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmchilton committed Mar 28, 2024
1 parent dc1870c commit 5e02933
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
27 changes: 27 additions & 0 deletions client/src/components/Markdown/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,33 @@ describe("parse.ts", () => {
expect(result).toBe(output);
});

it("should work with double quotes for labels and spaces in the quotes", () => {
const input =
"some random\n`markdown content`\n```galaxy\nhistory_dataset_embedded(footer='cow', output=\"from this\", title=dog)\n```\n";
const output =
"some random\n`markdown content`\n```galaxy\nhistory_dataset_embedded(footer='cow', output=\"to that\", title=dog)\n```\n";
const result = replaceLabel(input, "output", "from this", "to that");
expect(result).toBe(output);
});

it("should work with single quotes for labels and spaces in the quotes", () => {
const input =
"some random\n`markdown content`\n```galaxy\nhistory_dataset_embedded(footer='cow', output='from this', title=dog)\n```\n";
const output =
"some random\n`markdown content`\n```galaxy\nhistory_dataset_embedded(footer='cow', output='to that', title=dog)\n```\n";
const result = replaceLabel(input, "output", "from this", "to that");
expect(result).toBe(output);
});

it("should add quotes when refactoring to labels with spaces", () => {
const input =
"some random\n`markdown content`\n```galaxy\nhistory_dataset_embedded(footer='cow', output=fromthis, title=dog)\n```\n";
const output =
"some random\n`markdown content`\n```galaxy\nhistory_dataset_embedded(footer='cow', output='to that', title=dog)\n```\n";
const result = replaceLabel(input, "output", "fromthis", "to that");
expect(result).toBe(output);
});

it("should leave non-arguments alone", () => {
const input =
"some random\n`markdown content`\n```galaxy\nhistory_dataset_embedded(title='cow from farm', output=from)\n```\n";
Expand Down
28 changes: 26 additions & 2 deletions client/src/components/Markdown/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ type Section = DefaultSection | GalaxyDirectiveSection;

type WorkflowLabelKind = "input" | "output" | "step";

const SINGLE_QUOTE = "'";
const DOUBLE_QUOTE = '"';

export function splitMarkdown(markdown: string, preserveWhitespace: boolean = false) {
const sections: Section[] = [];
const markdownErrors = [];
Expand Down Expand Up @@ -78,8 +81,20 @@ export function replaceLabel(
const newArgs = { ...args };
newArgs[labelType] = toLabel;
const argRexExp = namedArgumentRegex(labelType);
const escapedToLabel = escapeRegExpReplacement(toLabel);
const content = directiveSection.content.replace(argRexExp, `$1${escapedToLabel}`);
let escapedToLabel = escapeRegExpReplacement(toLabel);
let incomingContent = directiveSection.content;
let content: string;
const match = incomingContent.match(argRexExp);
if (match) {
const firstMatch = match[0];
if (escapedToLabel.indexOf(" ") >= 0) {
const quoteChar = getQuoteChar(firstMatch);
escapedToLabel = `${quoteChar}${escapedToLabel}${quoteChar}`;
}
content = incomingContent.replace(argRexExp, `$1${escapedToLabel}`);
} else {
content = incomingContent;
}
return {
name: directiveSection.name,
args: newArgs,
Expand All @@ -95,6 +110,15 @@ export function replaceLabel(
return rewrittenMarkdown;
}

function getQuoteChar(argMatch: string): string {
// this could be a lot stronger, handling escaping and such...
let quoteChar = SINGLE_QUOTE;
if (argMatch.indexOf(DOUBLE_QUOTE) >= 0) {
quoteChar = DOUBLE_QUOTE;
}
return quoteChar;
}

export function getArgs(content: string): GalaxyDirectiveSection {
const galaxy_function = FUNCTION_CALL_LINE_TEMPLATE.exec(content);
if (galaxy_function == null) {
Expand Down

0 comments on commit 5e02933

Please sign in to comment.