From 54beb6afc40fc038e5dc3b76bc3a9139e05a3d61 Mon Sep 17 00:00:00 2001 From: aboyko Date: Mon, 25 Nov 2024 14:06:23 -0500 Subject: [PATCH] Escape parenthesis for links in Markdown format --- .../ide/vscode/commons/util/Renderables.java | 3 ++- .../vscode/commons/util/RenderablesTest.java | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 headless-services/commons/commons-util/src/test/java/org/springframework/ide/vscode/commons/util/RenderablesTest.java diff --git a/headless-services/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/Renderables.java b/headless-services/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/Renderables.java index 22db55735..a6b1e3310 100644 --- a/headless-services/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/Renderables.java +++ b/headless-services/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/Renderables.java @@ -210,7 +210,8 @@ public class Renderables { buffer.append(']'); if (url != null) { buffer.append('('); - buffer.append(url); + // Escape parenthesis for the MD format + buffer.append(url.replace("(", "%28").replace(")", "%29")); buffer.append(')'); } } diff --git a/headless-services/commons/commons-util/src/test/java/org/springframework/ide/vscode/commons/util/RenderablesTest.java b/headless-services/commons/commons-util/src/test/java/org/springframework/ide/vscode/commons/util/RenderablesTest.java new file mode 100644 index 000000000..40949b926 --- /dev/null +++ b/headless-services/commons/commons-util/src/test/java/org/springframework/ide/vscode/commons/util/RenderablesTest.java @@ -0,0 +1,27 @@ +/******************************************************************************* + * Copyright (c) 2024 Broadcom, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Broadcom, Inc. - initial API and implementation + *******************************************************************************/ +package org.springframework.ide.vscode.commons.util; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; + +public class RenderablesTest { + + @Test + void escapeParenthesisForMardownLink() { + Renderable r = Renderables.link("my-link-with-parenthesis", "https://foo.com/index(1).html"); + StringBuilder sb = new StringBuilder(); + r.renderAsMarkdown(sb); + assertThat(sb.toString()).isEqualTo("[my-link-with-parenthesis](https://foo.com/index%281%29.html)"); + } + +}