diff --git a/.changeset/four-coats-float.md b/.changeset/four-coats-float.md new file mode 100644 index 0000000..06587b6 --- /dev/null +++ b/.changeset/four-coats-float.md @@ -0,0 +1,5 @@ +--- +"osrs-web-scraper": patch +--- + +Update @osrs-wiki/mediawiki-builder diff --git a/.changeset/slow-comics-admire.md b/.changeset/slow-comics-admire.md new file mode 100644 index 0000000..3c5877d --- /dev/null +++ b/.changeset/slow-comics-admire.md @@ -0,0 +1,5 @@ +--- +"osrs-web-scraper": patch +--- + +Update table node parser to support new mediawiki-builder table format diff --git a/package.json b/package.json index 798a0d8..231ec5e 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/scrapers/news/sections/newsContent/nodes/__tests__/__snapshots__/table.test.ts.snap b/src/scrapers/news/sections/newsContent/nodes/__tests__/__snapshots__/table.test.ts.snap new file mode 100644 index 0000000..c9f2f37 --- /dev/null +++ b/src/scrapers/news/sections/newsContent/nodes/__tests__/__snapshots__/table.test.ts.snap @@ -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 +|}" +`; diff --git a/src/scrapers/news/sections/newsContent/nodes/__tests__/table.test.ts b/src/scrapers/news/sections/newsContent/nodes/__tests__/table.test.ts new file mode 100644 index 0000000..777f755 --- /dev/null +++ b/src/scrapers/news/sections/newsContent/nodes/__tests__/table.test.ts @@ -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( + "
testtest
testtest
" + ); + const builder = new MediaWikiBuilder(); + builder.addContents([tableParser(root.firstChild)].flat()); + expect(builder.build()).toMatchSnapshot(); + }); +}); diff --git a/src/scrapers/news/sections/newsContent/nodes/table.ts b/src/scrapers/news/sections/newsContent/nodes/table.ts index de93130..5817ad1 100644 --- a/src/scrapers/news/sections/newsContent/nodes/table.ts +++ b/src/scrapers/news/sections/newsContent/nodes/table.ts @@ -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"; @@ -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((node) => ({ + content: [new MediaWikiText(node.textContent.trim())], + })); + const tableRows: MediaWikiTableRow[] = rowNodes.map( + (trNode) => { + const tdNodes = trNode.querySelectorAll("td"); + return { + cells: tdNodes.map((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, + ], }); } }; diff --git a/yarn.lock b/yarn.lock index ffb9ec3..91b47b1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"