aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorLMBishop <13875753+LMBishop@users.noreply.github.com>2021-12-22 11:55:22 +0000
committerLMBishop <13875753+LMBishop@users.noreply.github.com>2021-12-22 11:55:22 +0000
commit4ddf6fc9ed90a704869da683561e5032b6c48d79 (patch)
tree074e78694818e34d37f4ff76a26f87fe68a0489a /app
parente926b5bcaf6035c82b9fb493d9d278946e5f97dd (diff)
Convert wiki parser to typescript
Diffstat (limited to 'app')
-rw-r--r--app/directory.ts2
-rw-r--r--app/wikiparser.ts (renamed from app/wikiparser.mjs)36
2 files changed, 23 insertions, 15 deletions
diff --git a/app/directory.ts b/app/directory.ts
index c5c42e7..fd9c008 100644
--- a/app/directory.ts
+++ b/app/directory.ts
@@ -1,4 +1,4 @@
-import { parse } from './wikiparser.mjs';
+import { parse } from './wikiparser.js';
import { readFileSync } from 'fs';
import glob from 'glob';
diff --git a/app/wikiparser.mjs b/app/wikiparser.ts
index a912065..544b6e5 100644
--- a/app/wikiparser.mjs
+++ b/app/wikiparser.ts
@@ -21,7 +21,16 @@
*/
import dateFormat from 'dateformat';
import htmlEscape from 'escape-html';
-import * as fs from 'fs';
+
+export class Result {
+ public html: string;
+ public metadata: any;
+
+ constructor(html: string, metadata: any) {
+ this.html = html;
+ this.metadata = metadata;
+ }
+}
const re = (regex, flag = 'mgi') => {
return RegExp(regex.replace(/ /g, '').replace(/\|\|.+?\|\|/g, ''), flag);
@@ -29,14 +38,15 @@ const re = (regex, flag = 'mgi') => {
const r = String.raw;
const arg = r`\s*([^|}]+?)\s*`;
-export function parse(directory, data) {
+export function parse(directory, data): Result {
const vars = {};
- const metadata = {};
- let nowikis = [];
+ const metadata: any = {};
+ const nowikis = [];
+ const refs = [];
+
let nowikiCount = 0;
let rawExtLinkCount = 0;
let refCount = 0;
- let refs = [];
let outText = data;
@@ -132,10 +142,10 @@ export function parse(directory, data) {
// Substitite arguments
const argMatch = (arg) => re(r`{{{ \s* ${arg} (?:\|([^}]*))? \s* }}}`);
- let args = params.split('|').slice(1);
- for (let i in args) {
- let parts = args[i].split('=');
- let [arg, val] = parts[1] ? [parts[0], ...parts.slice(1)] : [(+i + 1) + '', parts[0]];
+ const args = params.split('|').slice(1);
+ for (const i in args) {
+ const parts = args[i].split('=');
+ const [arg, val] = parts[1] ? [parts[0], ...parts.slice(1)] : [(+i + 1) + '', parts[0]];
content = content.replace(argMatch(arg), (_, m) => val || m || '');
}
for (let i = 1; i <= 10; i++) {
@@ -150,8 +160,8 @@ export function parse(directory, data) {
if (/{{/.test(params)) return _;
const path = file.trim().replace(/ /g, '_');
let caption = '';
- let imageData = {};
- let imageArgs = params?.split('|').map((arg) => arg.replace(/"/g, '&quot;'));
+ const imageData: any = {};
+ const imageArgs = params?.split('|').map((arg) => arg.replace(/"/g, '&quot;'));
if (imageArgs) {
for (const param of imageArgs) {
if (['left', 'right', 'center', 'none'].includes(param)) {
@@ -268,8 +278,6 @@ export function parse(directory, data) {
}
metadata.buildTime = new Date();
- let result = {};
- result.html = outText;
- result.metadata = metadata;
+ const result = new Result(outText, metadata);
return result;
}