Skip to content

Commit

Permalink
MagicWizardsParser.js v0.72 - Generalise title selection
Browse files Browse the repository at this point in the history
Also add TODO to JS. 2024 site and pre-2018 site work and are priority, as they cover all modern stories and older lost chapters. (Ancient MTG articles from pre-2014 not accounted for yet)
  • Loading branch information
Darthagnon committed Sep 22, 2024
1 parent bfda6d9 commit fd8c87f
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions plugin/js/parsers/MagicWizardsParser.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
/*
parser for mtgstory.com (redirect)
MagicWizardsParser.js v0.72
Parser for Magic the Gathering fiction, found on:
- mtgstory.com (redirect)
- https://magic.wizards.com/en/story (2023-2024)
- https://magic.wizards.com/en/articles/columns/magic-story (2014-2018)
- Archive.org versions of the above
- TODO: mtglore.com (redirects & mirrors)
- TODO: https://magic.wizards.com/en/story (Q4 2018-2022)
- TODO: Planeswalkers & Planes Databank
- TODO: Featured story slider Q1 2018
- UNTESTED: http://www.wizards.com/Magic/Magazine/Article.aspx (2014 and earlier)
- WONTFIX: hanweirchronicle.com (Tumblr blog, mostly image posts)
*/
"use strict";

// Register the parser for magic.wizards.com (archive.org is implicit) TODO: mtglore.com
// Register the parser for magic.wizards.com (archive.org is implicit)
parserFactory.register("magic.wizards.com", () => new MagicWizardsParser());
//parserFactory.register("mtglore.com", () => new MagicWizardsParser());

class MagicWizardsParser extends Parser {
constructor() {
Expand Down Expand Up @@ -36,14 +47,23 @@ class MagicWizardsParser extends Parser {

// Format chapter links into a standardized structure
linkToChapter(link) {
let titleElement;
const titleSelectors = [
"h3", // First option: <h3> tag
".article-item .title", // Second option: <p class="title">
".details .title" // Third option: <p class="title" inside .details>
];

// Try to find the <h3> tag inside the parent of the link (assuming link is inside <article>)
titleElement = link.closest("article")?.querySelector("h3");

// Fallback to the <p class="title"> if no <h3> is found
if (!titleElement) {
titleElement = link.closest(".article-item")?.querySelector(".title");
let titleElement = null;

// Iterate through the selectors and find the first matching element
for (const selector of titleSelectors) {
titleElement = link.closest("article")?.querySelector(selector) ||
link.closest(".article-item")?.querySelector(selector) ||
link.closest(".details")?.querySelector(selector);

if (titleElement) {
break; // Exit the loop if a title element is found
}
}

// Fallback to the link text itself if no titleElement found (this handles simpler cases)
Expand Down

0 comments on commit fd8c87f

Please sign in to comment.