-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Include basic parser and service
- Loading branch information
Showing
27 changed files
with
599 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
parser: character-origin | ||
extension: character-origin | ||
--- | ||
|
||
## Ceres und der Asteroidengürtel | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
parser: character-origin | ||
extension: character-origin | ||
--- | ||
|
||
## Silbernes Reich — Minenkolonien | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
parser: character-origin | ||
extension: character-origin | ||
--- | ||
|
||
## Silbernes Reich — Triton | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
parser: character-origin | ||
extension: character-origin | ||
--- | ||
|
||
## Erde — Abseits der Städte | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
parser: character-origin | ||
extension: character-origin | ||
--- | ||
|
||
## Erde — Großstädte und Raumstationen | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
parser: character-origin | ||
extension: character-origin | ||
--- | ||
|
||
## Europa | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
parser: character-origin | ||
extension: character-origin | ||
--- | ||
|
||
## Ganymed | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
parser: character-origin | ||
extension: character-origin | ||
--- | ||
|
||
## Mars | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
parser: character-origin | ||
extension: character-origin | ||
--- | ||
|
||
## Titan | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
parser: character-origin | ||
extension: character-origin | ||
--- | ||
|
||
## Venus | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './wiki.config'; | ||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export const WikiLocale = { | ||
de: 'de', | ||
} as const; | ||
export type WikiLocale = (typeof WikiLocale)[keyof typeof WikiLocale]; | ||
|
||
export interface WikiConfig { | ||
/** | ||
* The base URL of the wiki. | ||
*/ | ||
BASE_URL: string; | ||
/** | ||
* The locale of the wiki. | ||
*/ | ||
LOCALE: WikiLocale; | ||
/** | ||
* The headers to use for outgoing HTTP requests. | ||
*/ | ||
HTTP_HEADERS?: Record<string, string>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { WikiLocale, type WikiConfig } from './types'; | ||
|
||
export const wikiConfig: WikiConfig = { | ||
BASE_URL: 'https://raw.githubusercontent.com/satellite-games/new-horizons-wiki/main/books', | ||
LOCALE: WikiLocale.de, | ||
HTTP_HEADERS: {}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,18 @@ | ||
import { characterOriginParser } from './character-origin.parser'; | ||
// import { characterOriginParser } from '../services/parser/parsers/character-origin.parser'; | ||
|
||
/** | ||
* This is the collection of parsers that can be used to enrich articles provided by | ||
* `newhorizons-wiki` with in-game information. Each parser is a function that takes | ||
* takes a Markdown string as input and returns a Markdown string with the parsed information. | ||
* For more information on the wiki, see: https://github.com/satellite-games/newhorizons-wiki | ||
*/ | ||
export const wikiArticleParsers: Record<string, WikiArticleParserFunction> = { | ||
'character-origin': characterOriginParser, | ||
}; | ||
// /** | ||
// * This is the collection of parsers that can be used to enrich articles provided by | ||
// * `newhorizons-wiki` with in-game information. Each parser is a function that takes | ||
// * takes a Markdown string as input and returns a Markdown string with the parsed information. | ||
// * For more information on the wiki, see: https://github.com/satellite-games/newhorizons-wiki | ||
// */ | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type WikiArticleParserFunction = (article: string, ...args: any[]) => Promise<string>; | ||
|
||
/** | ||
* Parses the given wiki article and returns the parsed article. | ||
* @param article The article to parse. | ||
*/ | ||
export function parseWikiArticle(article: string, parser: keyof typeof wikiArticleParsers) { | ||
if (!wikiArticleParsers[parser]) return; | ||
const parsedArticle = wikiArticleParsers[parser](article); | ||
return parsedArticle; | ||
} | ||
// /** | ||
// * Parses the given wiki article and returns the parsed article. | ||
// * @param article The article to parse. | ||
// */ | ||
// export function parseWikiArticle(article: string, parser: keyof typeof wikiArticleParsers) { | ||
// if (!wikiArticleParsers[parser]) return; | ||
// const parsedArticle = wikiArticleParsers[parser](article); | ||
// return parsedArticle; | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './parser.service'; | ||
export * from './parser.registry'; |
4 changes: 2 additions & 2 deletions
4
lib/parsers/character-origin.parser.test.ts → ...r-extensions/character-origin.pex.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { ParserFunction } from '@/types/private-types'; | ||
import { characterOriginParser } from './parser-extensions/character-origin.pex'; | ||
|
||
export const parserRegistry: Record<string, ParserFunction> = { | ||
'character-origin': characterOriginParser, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { ServiceMixin } from '@spuxx/browser-utils'; | ||
import { marked } from 'marked'; | ||
import { sanitize } from 'isomorphic-dompurify'; | ||
import yaml from 'js-yaml'; | ||
import { WikiService } from '../wiki/wiki.service'; | ||
|
||
export class WikiParser extends ServiceMixin<WikiParser>() { | ||
frontmatterPattern = /^---\s*([\s\S]*?)\s*---/; | ||
|
||
/** | ||
* Parses the Markdown article and to HTML. | ||
* @param markdown The Markdown to parse. | ||
* @returns The parsed HTML. | ||
*/ | ||
public static async parse(article: string): Promise<string> { | ||
// const frontmatter = this.extractFrontmatter(article); | ||
let parsedArticle = this.removeFrontmatter(article); | ||
parsedArticle = this.doSimpleReplacements(parsedArticle); | ||
const html = await marked.parse(parsedArticle); | ||
const sanitizedHtml = sanitize(html); | ||
return sanitizedHtml; | ||
} | ||
|
||
/** | ||
* Extracts the frontmatter from a markdown string. | ||
* @param markdown The markdown string. | ||
* @returns The frontmatter as a JavaScript object. | ||
*/ | ||
public static extractFrontmatter(markdown: string): Record<string, unknown> | undefined { | ||
const match = markdown.match(new RegExp(this.instance.frontmatterPattern)); | ||
if (!match) return; | ||
const frontmatter = yaml.load(match[1]); | ||
return frontmatter as Record<string, unknown>; | ||
} | ||
|
||
/** | ||
* Removes the frontmatter from a markdown string. | ||
* @param markdown The markdown string. | ||
* @returns The markdown string without the frontmatter. | ||
*/ | ||
private static removeFrontmatter(markdown: string): string { | ||
return markdown.replace(new RegExp(this.instance.frontmatterPattern), ''); | ||
} | ||
|
||
private static doSimpleReplacements(article: string) { | ||
let parsedArticle = article; | ||
// Replace partial image urls will full urls | ||
const imageSrcPattern = /<img.*src=['"](\/books)/gi; | ||
const imageSrcMatches = parsedArticle.matchAll(imageSrcPattern); | ||
for (const match of imageSrcMatches) { | ||
parsedArticle = parsedArticle.replace(match[1], WikiService.config.BASE_URL); | ||
} | ||
return parsedArticle; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './wiki.service'; | ||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export interface WikiBookHeader { | ||
id: string; | ||
title: Record<string, string>; | ||
icon: string; | ||
default: { | ||
chapter: string; | ||
article: string; | ||
}; | ||
} | ||
|
||
export interface WikiBookToc extends WikiBookHeader { | ||
chapters?: { | ||
id: string; | ||
title: Record<string, string>; | ||
articles?: { | ||
id: string; | ||
title: Record<string, string>; | ||
}[]; | ||
}[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { wikiConfig, type WikiConfig } from '@/config'; | ||
import { ServiceMixin } from '@spuxx/browser-utils'; | ||
import yaml from 'js-yaml'; | ||
import { WikiParser } from '../parser'; | ||
import type { WikiBookHeader, WikiBookToc } from './types'; | ||
|
||
/** | ||
* Provides functionalities to interact with the wiki. | ||
*/ | ||
export class WikiService extends ServiceMixin<WikiService>() { | ||
private _config: WikiConfig = wikiConfig; | ||
|
||
static get config() { | ||
return this.instance._config; | ||
} | ||
|
||
/** | ||
* Updates the configuration of the wiki. Will merge the given | ||
* configuration wih the packages default configuration. | ||
* @param config The new configuration. | ||
*/ | ||
static setConfig(config: Partial<WikiConfig>) { | ||
this.instance._config = { ...wikiConfig, ...config }; | ||
} | ||
|
||
/** | ||
* Attempts to fetch an article and return it. | ||
* @param book The book id. | ||
* @param chapter The chapter id. | ||
* @param article The article id. | ||
* @returns The article. | ||
*/ | ||
public static async fetchArticle(book: string, chapter: string, article: string) { | ||
const url = `${this.config.BASE_URL}/${book}/${chapter}/${article}/${this.config.LOCALE}.md`; | ||
try { | ||
const response = await fetch(url, { headers: this.config.HTTP_HEADERS }); | ||
const text = await response.text(); | ||
const html = await WikiParser.parse(text); | ||
return html; | ||
} catch (error) { | ||
throw new Error(`Unable to retrieve article from '${url}'.`); | ||
} | ||
} | ||
|
||
/** | ||
* Attempts to fetch the list of all available books. | ||
* @returns The list of books. | ||
*/ | ||
public static async fetchBooks(): Promise<WikiBookHeader[]> { | ||
const url = `${this.config.BASE_URL}/books.yaml`; | ||
try { | ||
const response = await fetch(url); | ||
const text = await response.text(); | ||
const books = yaml.load(text) as WikiBookHeader[]; | ||
return books; | ||
} catch (error) { | ||
throw new Error(`Unable to retrieve list of books.`); | ||
} | ||
} | ||
|
||
/** | ||
* Attempts to fetch the table of contents for the Stellarpedia. | ||
* @param book The book id. | ||
* @returns The table of contents. | ||
*/ | ||
public static async fetchToc(book: string): Promise<WikiBookToc> { | ||
const url = `${this.config.BASE_URL}/${book}/toc.yaml`; | ||
try { | ||
const response = await fetch(url); | ||
const text = await response.text(); | ||
const toc = yaml.load(text) as WikiBookToc; | ||
return toc; | ||
} catch (error) { | ||
throw new Error(`Unable to retrieve table of contents for book '${book}'.`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type ParserFunction = (article: string, ...args: any[]) => Promise<string>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.