Skip to content
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

GoogleAction: Update description bug and support vertical tables #978

Merged
merged 5 commits into from
Feb 15, 2025
Merged
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
169 changes: 167 additions & 2 deletions core/actions/googleDriveImport/gdocPlugins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,26 @@ test("Convert double table", async () => {
expect(result).toStrictEqual(expectedOutput);
});

test("Convert vert table", async () => {
const inputNode = JSON.parse(
'{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"span","properties":{},"children":[{"type":"text","value":"Type"}]}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"span","properties":{},"children":[{"type":"text","value":"Image"}]}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"span","properties":{},"children":[{"type":"text","value":"Id"}]}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"span","properties":{},"children":[{"type":"text","value":"n8r4ihxcrly"}]}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"span","properties":{},"children":[{"type":"text","value":"Source"}]}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"span","properties":{},"children":[{"type":"text","value":"https://resize-v3.pubpub.org/123"}]}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"span","properties":{},"children":[{"type":"text","value":"Alt Text"}]}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"b","properties":{},"children":[{"type":"text","value":"123"}]}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"span","properties":{},"children":[{"type":"text","value":"Align"}]}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"full"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"span","properties":{},"children":[{"type":"text","value":"Size"}]}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"50"}]}]}]}]}]}'
);
const expectedOutput = [
{
type: "image",
id: "n8r4ihxcrly",
source: "https://resize-v3.pubpub.org/123",
alttext: "123",
align: "full",
size: "50",
},
];

const result = tableToObjectArray(inputNode);

expect(result).toStrictEqual(expectedOutput);
});

test("Do Nothing", async () => {
const inputHtml =
'<html><head><script src="blah.js"></script><style>.blah{}</style></head><body><div>Content</div></body></html>';
Expand Down Expand Up @@ -187,6 +207,151 @@ test("Structure Images", async () => {

expect(trimAll(result)).toBe(trimAll(expectedOutputHtml));
});
test("Structure Images - Vert Table", async () => {
const inputHtml = `
<html>
<head></head>
<body>
<table>
<tbody>
<tr>
<td><p><span>Type</span></p></td>
<td><p><span>Image</span></p></td>
</tr>
<tr>
<td><p><span>Id</span></p></td>
<td><p><span>n8r4ihxcrly</span></p></td>
</tr>
<tr>
<td><p><span>Source</span></p></td>
<td><p><span>https://resize-v3.pubpub.org/123</span></p></td>
</tr>
<tr>
<td><p><span>Caption</span></p></td>
<td><p><span>With a caption. </span><b>Bold</b></p></td>
</tr>
<tr>
<td><p><span>Alt Text</span></p></td>
<td><p><b>123</b></p></td>
</tr>
<tr>
<td><p><span>Align</span></p></td>
<td><p>full</p></td>
</tr>
<tr>
<td><p><span>Size</span></p></td>
<td><p>50</p></td>
</tr>
</tbody>
</table>
</body>
</html>
`;
const expectedOutputHtml = `
<html>
<head></head>
<body>
<figure data-figure-type="img" id="n8r4ihxcrly" data-align="full" data-size="50">
<img alt="123" src="https://resize-v3.pubpub.org/123">
<figcaption>
<p>
<span>With a caption. </span>
<b>Bold</b>
</p>
</figcaption>
</figure>
</body>
</html>
`;

const result = await rehype()
.use(structureImages)
.process(inputHtml)
.then((file) => String(file))
.catch((error) => {
logger.error(error);
});

expect(trimAll(result)).toBe(trimAll(expectedOutputHtml));
});
test("Structure Images - DoubleVert Table", async () => {
const inputHtml = `
<html>
<head></head>
<body>
<table>
<tbody>
<tr>
<td><p><span>Type</span></p></td>
<td><p><span>Image</span></p></td>
<td><p><span>Image</span></p></td>
</tr>
<tr>
<td><p><span>Id</span></p></td>
<td><p><span>n8r4ihxcrly</span></p></td>
<td><p><span>abr4ihxcrly</span></p></td>
</tr>
<tr>
<td><p><span>Source</span></p></td>
<td><p><span>https://resize-v3.pubpub.org/123</span></p></td>
<td><p><span>https://resize-v4.pubpub.org/123</span></p></td>
</tr>
<tr>
<td><p><span>Caption</span></p></td>
<td><p><span>With a caption. </span><b>Bold</b></p></td>
<td><p></p></td>
</tr>
<tr>
<td><p><span>Alt Text</span></p></td>
<td><p><b>123</b></p></td>
<td><p>abc</p></td>
</tr>
<tr>
<td><p><span>Align</span></p></td>
<td><p>full</p></td>
<td><p>left</p></td>
</tr>
<tr>
<td><p><span>Size</span></p></td>
<td><p>50</p></td>
<td><p>75</p></td>
</tr>
</tbody>
</table>
</body>
</html>
`;
const expectedOutputHtml = `
<html>
<head></head>
<body>
<figure data-figure-type="img" id="n8r4ihxcrly" data-align="full" data-size="50">
<img alt="123" src="https://resize-v3.pubpub.org/123">
<figcaption>
<p>
<span>With a caption. </span>
<b>Bold</b>
</p>
</figcaption>
</figure>
<figure data-figure-type="img" id="abr4ihxcrly" data-align="left" data-size="75">
<img alt="abc" src="https://resize-v4.pubpub.org/123">
<figcaption><p></p></figcaption>
</figure>
</body>
</html>
`;

const result = await rehype()
.use(structureImages)
.process(inputHtml)
.then((file) => String(file))
.catch((error) => {
logger.error(error);
});

expect(trimAll(result)).toBe(trimAll(expectedOutputHtml));
});

test("Structure Images", async () => {
const inputHtml = `
Expand Down Expand Up @@ -1060,14 +1225,14 @@ test("getDescription", async () => {
</tr>
<tr>
<td>Description</td>
<td>Seeing how microbes are organized ...</td>
<td>Seeing how <i>microbes</i> are organized ...</td>
</tr>
</table><p>Hello</p>
</body>
</html>

`;
const expectedOutputHtml = `Seeing how microbes are organized ...`;
const expectedOutputHtml = `Seeing how <i>microbes</i> are organized ...`;

const result = getDescription(inputHtml);

Expand Down
112 changes: 90 additions & 22 deletions core/actions/googleDriveImport/gdocPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,33 +60,93 @@ export const tableToObjectArray = (node: any) => {
const tbody = node.children.find((child: any) => child.tagName === "tbody");
if (!tbody) return [{ type: "empty" }];

const rows: Element[] = tbody.children.filter((child: any) => child.tagName === "tr");
const rows: Element[] = tbody.children
.filter((child: any) => child.tagName === "tr")
.map((row: any) => {
return {
...row,
children: row.children.filter((child: any) => {
return child.tagName === "td";
}),
};
});
if (rows.length === 0) return [{ type: "empty" }];

const headers: string[] = rows[0].children
const headersHoriz: string[] = rows[0].children
.filter((child: any) => child.tagName === "td")
.map((header: any) => getTextContent(header).toLowerCase().replace(/\s+/g, ""));

const data = rows.slice(1).map((row: any) => {
const cells = row.children.filter((child: any) => child.tagName === "td");
const obj: { [key: string]: any } = {};
const typeIndex = headers.findIndex((header) => header === "type");
const tableType = getTextContent(cells[typeIndex]).trim().toLowerCase().replace(/\s+/g, "");
cells.forEach((cell: any, index: number) => {
if (
(!["math", "reference", "description"].includes(tableType) &&
headers[index] === "value") ||
headers[index] === "caption"
) {
obj[headers[index]] = cell.children;
} else {
obj[headers[index]] = getTextContent(cell).trim();
}
});

return { ...obj, type: tableType };
});
const headersVert: string[] = rows.map((row: any) =>
getTextContent(row.children[0]).toLowerCase().replace(/\s+/g, "")
);
const validTypes = [
"image",
"video",
"audio",
"file",
"iframe",
"blockquote",
"code",
"math",
"anchor",
"reference",
"footnote",
"description",
];

const isHoriz = validTypes.includes(headersVert[1].toLowerCase());
const isVert = validTypes.includes(headersHoriz[1].toLowerCase());

let data;
if (isHoriz) {
data = rows.slice(1).map((row: any) => {
const cells = row.children.filter((child: any) => child.tagName === "td");
const obj: { [key: string]: any } = {};
const typeIndex = headersHoriz.findIndex((header) => header === "type");
const tableType = getTextContent(cells[typeIndex])
.trim()
.toLowerCase()
.replace(/\s+/g, "");
cells.forEach((cell: any, index: number) => {
if (
(!["math", "reference"].includes(tableType) &&
headersHoriz[index] === "value") ||
headersHoriz[index] === "caption"
) {
obj[headersHoriz[index]] = cell.children;
} else {
obj[headersHoriz[index]] = getTextContent(cell).trim();
}
});

return { ...obj, type: tableType };
});
} else if (isVert) {
data = headersHoriz.slice(1).map((_header, colIndex) => {
const obj: { [key: string]: any } = {};
const typeIndex = headersVert.findIndex((header) => header === "type");
const tableType = getTextContent(rows[typeIndex].children[colIndex + 1])
.trim()
.toLowerCase()
.replace(/\s+/g, "");

rows.forEach((row: any, rowIndex: number) => {
const cell = row.children[colIndex + 1];
if (
(!["math", "reference"].includes(tableType) &&
headersVert[rowIndex] === "value") ||
headersVert[rowIndex] === "caption"
) {
obj[headersVert[rowIndex]] = cell.children;
} else {
obj[headersVert[rowIndex]] = getTextContent(cell).trim();
}
});
return { ...obj, type: tableType };
});
} else {
return [{ type: "empty" }];
}
return data;
};

Expand All @@ -99,7 +159,15 @@ export const getDescription = (htmlString: string): string => {
const tableData: any = tableToObjectArray(node);
const tableType = tableData[0].type;
if (tableType === "description") {
description = tableData[0].value;
const valueFragment: Element = {
type: "element",
tagName: "span",
properties: {},
children: tableData[0].value,
};
description = rehypeFragmentToHtmlString(valueFragment)
.replace("<span>", "")
.replace("</span>", "");
return false;
}
}
Expand Down
Loading