Skip to content

prevent markdown table from breaking on formatted nodes #5560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions extensions/vscode/e2e/selectors/GUI.selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ export class GUISelectors {
}

public static getThreadMessageByText(view: WebView, text: string) {
void view.findElement(By.xpath(`//*[@class="thread-message"]`)).then(e=>{
console.log('debug1 thread element: ', e);
});

void view.findElement(By.xpath(`//*[@class="thread-message"]`)).getText().then(e=>{
console.log('debug1 thread message: ', e);
});

return view.findWebElement(
By.xpath(`//*[@class="thread-message"]//*[contains(text(), "${text}")]`),
);
Expand Down
3 changes: 2 additions & 1 deletion extensions/vscode/e2e/tests/GUI.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,8 @@ describe("GUI Test", () => {
});

describe("Repeat back the system message", () => {
it("should repeat back the system message", async () => {
it("should repeat back the system message", async function () {
this.timeout(DEFAULT_TIMEOUT.XL);
await GUIActions.selectModelFromDropdown(view, "SYSTEM MESSAGE MOCK LLM");
const [messageInput] = await GUISelectors.getMessageInputFields(view);
await messageInput.sendKeys("Hello");
Expand Down
7 changes: 7 additions & 0 deletions gui/src/components/StyledMarkdownPreview/markdown.css
Original file line number Diff line number Diff line change
Expand Up @@ -1037,3 +1037,10 @@ body[data-color-mode*="light"] {
.token.entity {
cursor: help;
}
/*
.markdown-table th {
background-color: var(--color-fg-muted);
}
.markdown-table td {
background-color: var(--color-fg-subtle);
} */
26 changes: 17 additions & 9 deletions gui/src/components/StyledMarkdownPreview/utils/remarkTables.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ import { visit } from "unist-util-visit";
*/
export function remarkTables() {
return (tree: any) => {
visit(tree, "text", (node, index, parent) => {
const { value } = node;
visit(tree, "paragraph", (paragraphNode, index, parentOfParagraphNode) => {
let buffer = "";
visit(paragraphNode, "text", (textNode) => {
buffer += textNode.value;
});

const tableRegex =
/((?:\| *[^|\r\n]+ *)+\|)(?:\r?\n)((?:\|[ :]?-+[ :]?)+\|)((?:(?:\r?\n)(?:\| *[^|\r\n]+ *)+\|)+)/g;
Expand All @@ -33,7 +36,8 @@ export function remarkTables() {
let lastIndex = 0;
const newNodes = [];
let failed = false;
while ((match = tableRegex.exec(value)) !== null) {

while ((match = tableRegex.exec(buffer)) !== null) {
const fullTableString = match[0];
const headerGroup = match[1];
const separatorGroup = match[2];
Expand Down Expand Up @@ -62,6 +66,11 @@ export function remarkTables() {
const tableNode = {
type: "table",
align: alignments,
data: {
hProperties: {
class: "markdown-table",
},
},
children: [
{
type: "tableRow",
Expand All @@ -77,7 +86,6 @@ export function remarkTables() {
type: "tableRow",
data: {
hProperties: {
class: "markdown-table",
key: i,
},
},
Expand All @@ -95,7 +103,7 @@ export function remarkTables() {
if (match.index > lastIndex) {
newNodes.push({
type: "text",
value: value.slice(lastIndex, match.index),
value: buffer.slice(lastIndex, match.index),
});
}

Expand All @@ -117,16 +125,16 @@ export function remarkTables() {
}

// Add any remaining text after the last table
if (lastIndex < value.length) {
if (lastIndex < buffer.length) {
newNodes.push({
type: "text",
value: value.slice(lastIndex),
value: buffer.slice(lastIndex),
});
}

// Replace the original text node with the new nodes
// Replace the original paragraph node with the new nodes
if (newNodes.length > 0) {
parent.children.splice(index, 1, ...newNodes);
parentOfParagraphNode.children.splice(index, 1, ...newNodes);
}
});
};
Expand Down
Loading