Skip to content

Commit

Permalink
Update mediawiki-builder version
Browse files Browse the repository at this point in the history
  • Loading branch information
allenkinzalow committed Mar 2, 2024
1 parent 24ff306 commit f4c086a
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-coats-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"osrs-web-scraper": patch
---

Update @osrs-wiki/mediawiki-builder
5 changes: 5 additions & 0 deletions .changeset/slow-comics-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"osrs-web-scraper": patch
---

Update table node parser to support new mediawiki-builder table format
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"homepage": "https://github.com/allenkinzalow/osrs-web-scraper#readme",
"dependencies": {
"@osrs-wiki/mediawiki-builder": "^1.1.2",
"@osrs-wiki/mediawiki-builder": "^1.2.1",
"date-fns": "^2.30.0",
"dotenv": "^16.0.3",
"image-size": "^1.0.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`table node A basic table should render 1`] = `
"{| style=\\"text-align: center;\\" class=\\"wikitable\\"
|-
! test
! test
|-
| test
| test
|}"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MediaWikiBuilder } from "@osrs-wiki/mediawiki-builder";
import parse from "node-html-parser";

import tableParser from "../table";

describe("table node", () => {
test("A basic table should render", () => {
const root = parse(
"<table><tbody><tr><td>test</td><td>test</td></tr><tr><td>test</td><td>test</td></tr></tbody></table>"
);
const builder = new MediaWikiBuilder();
builder.addContents([tableParser(root.firstChild)].flat());
expect(builder.build()).toMatchSnapshot();
});
});
57 changes: 38 additions & 19 deletions src/scrapers/news/sections/newsContent/nodes/table.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { MediaWikiTable } from "@osrs-wiki/mediawiki-builder";
import { MediaWikiTable, MediaWikiText } from "@osrs-wiki/mediawiki-builder";
import type {
MediaWikiTableCell,
MediaWikiTableRow,
} from "@osrs-wiki/mediawiki-builder";
import { HTMLElement } from "node-html-parser";

import nodeParser from "./parser";
Expand All @@ -11,25 +15,40 @@ export const tableParser: ContentNodeParser = (node, options) => {
const tbody = table.querySelector("tbody");
const rowNodes = tbody.querySelectorAll("tr");
const headerRowNodes = rowNodes.shift().querySelectorAll("td");
const headers: string[] = headerRowNodes.map((node) =>
node.textContent.trim()
const headers: MediaWikiTableCell[] =
headerRowNodes.map<MediaWikiTableCell>((node) => ({
content: [new MediaWikiText(node.textContent.trim())],
}));
const tableRows: MediaWikiTableRow[] = rowNodes.map<MediaWikiTableRow>(
(trNode) => {
const tdNodes = trNode.querySelectorAll("td");
return {
cells: tdNodes.map<MediaWikiTableCell>((tdNode) => ({
content: tdNode.childNodes
.map((childNode) => {
if (childNode instanceof HTMLElement) {
return nodeParser(childNode, options);
}
return textParser(childNode, options);
})
.flat(),
})),
};
}
);
const tableRows = rowNodes.map((trNode) => {
const tdNodes = trNode.querySelectorAll("td");
return tdNodes.map((tdNode) =>
tdNode.childNodes
.map((childNode) => {
if (childNode instanceof HTMLElement) {
return nodeParser(childNode, options);
}
return textParser(childNode, options);
})
.flat()
);
});
return new MediaWikiTable(tableRows, {
center: options.center as boolean,
headers,

return new MediaWikiTable({
options: {
style: "text-align: center;",
class: "wikitable",
},
rows: [
{
header: true,
cells: headers,
},
...tableRows,
],
});
}
};
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -929,10 +929,10 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"

"@osrs-wiki/mediawiki-builder@^1.1.2":
version "1.1.2"
resolved "https://registry.yarnpkg.com/@osrs-wiki/mediawiki-builder/-/mediawiki-builder-1.1.2.tgz#3e5e0db94b3a4d3b9fcf3e6f1ee6fbec4a110a79"
integrity sha512-L1zEApxhcYUwVPOpil8QsputnNDaa0YSuO+MFLdUtvi1XA9++elLFqtuHN5sOjI49uAJr58hvYCxurBrw0JeIw==
"@osrs-wiki/mediawiki-builder@^1.2.1":
version "1.2.1"
resolved "https://registry.yarnpkg.com/@osrs-wiki/mediawiki-builder/-/mediawiki-builder-1.2.1.tgz#79cfb157097c560870709c146eb50c13fc4c8053"
integrity sha512-oGWSWbmUP8n5WIpGWhPe3IIfbnbs+egLw7GBrsZFklZDyYN4uDPmMt163iqwExQD/s2tSA1n8M1fWNQA1XPZJg==
dependencies:
tslib "^2.6.2"

Expand Down

0 comments on commit f4c086a

Please sign in to comment.