|
1 | 1 | import { JSONToXD } from "./JSONtoXD" |
2 | | -import type { Clue, Position as CluePosition, CrosswordJSON } from "xd-crossword-tools-parser" |
| 2 | +import type { Clue, Position as CluePosition, CrosswordJSON, Report } from "xd-crossword-tools-parser" |
3 | 3 |
|
4 | 4 | import type { CellInfo, PlacedWord, AmuseTopLevel } from "./amuseJSONToXD.types.d.ts" |
5 | 5 |
|
@@ -207,11 +207,31 @@ export function convertAmuseToCrosswordJSON(amuseJson: AmuseTopLevel): Crossword |
207 | 207 | const cluesStructure: Clues = { across: [], down: [] } |
208 | 208 | const cluePositionsMap: Record<string, CluePosition> = {} |
209 | 209 |
|
| 210 | + const errorReports: Report[] = [] |
| 211 | + |
210 | 212 | amuseData.placedWords.forEach((placedWord: PlacedWord) => { |
211 | | - const direction = placedWord.clueSection === "Across" ? "across" : "down" |
| 213 | + let direction: "across" | "down" | null = null |
| 214 | + |
| 215 | + // I think different ages of amuse json's have different data structures, |
| 216 | + // as one crossword we have does not include "clueSection" - so we fall back to "acrossNotDown" |
| 217 | + if (placedWord.clueSection) direction = placedWord.clueSection === "Across" ? "across" : "down" |
| 218 | + if ("acrossNotDown" in placedWord && !direction) direction = placedWord.acrossNotDown ? "across" : "down" |
| 219 | + if (!direction) { |
| 220 | + errorReports.push({ |
| 221 | + type: "syntax", |
| 222 | + message: `Could not determine a direction for placed word ${placedWord.word}.`, |
| 223 | + length: -1, |
| 224 | + position: { |
| 225 | + col: placedWord.x, |
| 226 | + index: placedWord.y, |
| 227 | + }, |
| 228 | + }) |
| 229 | + return |
| 230 | + } |
| 231 | + |
212 | 232 | const clueText = convertHtmlToXdMarkup(placedWord.clue.clue) |
213 | 233 | const clueNumberStr = placedWord.clueNum // This is already a string from AmuseData |
214 | | - const word = placedWord.word || "" |
| 234 | + const word = placedWord.word || placedWord.originalTerm || "" |
215 | 235 | let answer |
216 | 236 | let alt |
217 | 237 |
|
@@ -239,7 +259,7 @@ export function convertAmuseToCrosswordJSON(amuseJson: AmuseTopLevel): Crossword |
239 | 259 | body: clueText, |
240 | 260 | answer: answer, |
241 | 261 | tiles: [], |
242 | | - direction: direction, |
| 262 | + direction, |
243 | 263 | display: [], |
244 | 264 | position: { |
245 | 265 | col: placedWord.x, |
@@ -408,9 +428,16 @@ export function convertHtmlToXdMarkup(html: string | undefined): string { |
408 | 428 | ) |
409 | 429 | } |
410 | 430 |
|
| 431 | + // Don't think this is actually a goal: https://github.com/puzzmo-com/xd-crossword-tools/issues/46 |
| 432 | + // Convert spaced dots to ellipsis |
| 433 | + // result = result.replace(/\. \. \./g, "…") |
| 434 | + |
411 | 435 | // Clean up excessive whitespace and newlines |
412 | 436 | result = result.replace(/\n+/g, "\n").trim() |
413 | 437 |
|
| 438 | + // Remove extra HTML entities like "'" |
| 439 | + result = result.replace(/&#(\d+);/g, (match, dec) => String.fromCharCode(dec)) |
| 440 | + |
414 | 441 | return result |
415 | 442 | } |
416 | 443 |
|
|
0 commit comments