(ts) migrate src/parseMarkdown.ts

This commit is contained in:
Valentyne Stigloher 2024-07-05 18:30:17 +02:00
parent ddd074b2bc
commit ead6f33a7c
4 changed files with 16 additions and 15 deletions

View File

@ -1,5 +1,5 @@
import { Feed } from 'feed';
import parseMarkdown from '../src/parseMarkdown.js';
import parseMarkdown from '../src/parseMarkdown.ts';
import fetch from 'node-fetch';
export default async function ({ app, route, res }) {

View File

@ -29,7 +29,7 @@
<script lang="ts">
import Vue from 'vue';
import { head } from '../src/helpers.ts';
import parseMarkdown from '../src/parseMarkdown.js';
import parseMarkdown from '../src/parseMarkdown.ts';
import Columnist from 'avris-columnist';
export default Vue.extend({

View File

@ -15,7 +15,7 @@
<script lang="ts">
import Vue from 'vue';
import { head } from '../src/helpers.ts';
import parseMarkdown from '../src/parseMarkdown.js';
import parseMarkdown from '../src/parseMarkdown.ts';
export default Vue.extend({
async asyncData({ app }) {

View File

@ -1,10 +1,11 @@
import { fetchJson } from './fetchJson.js';
import type { Translator } from './translator.ts';
let census_groups = {};
let census_comparisons = {};
let census_groups: Record<string, string> = {};
let census_comparisons: Record<string, string> = {};
const mainPlusDetails = (dict, wide) => (_, keys, content) => {
let selectedDict = {};
const mainPlusDetails = (dict: Record<string, string>, wide: boolean) => (_: string, keys: string, content: string) => {
let selectedDict: Record<string, string> = {};
if (keys === undefined) {
selectedDict = dict;
} else {
@ -25,12 +26,12 @@ const mainPlusDetails = (dict, wide) => (_, keys, content) => {
}</div>`;
};
const generateToC = (content, translator) => (_) => {
const generateToC = (content: string, translator: Translator) => (_: string) => {
const tags = [];
let curentLevel = 2;
let needsClosing = false;
for (let [, level, id, title] of content.matchAll(/<h([2-6]) id="([^"]+)">([^<]+)<\/h\1>/g)) {
level = parseInt(level);
for (const [, levelString, id, title] of content.matchAll(/<h([2-6]) id="([^"]+)">([^<]+)<\/h\1>/g)) {
const level = parseInt(levelString);
while (level < curentLevel) {
tags.push('</li>'); tags.push('</ul>'); curentLevel--;
}
@ -61,10 +62,10 @@ const generateToC = (content, translator) => (_) => {
`;
};
const generateGallery = (_, itemsString) => {
const items = JSON.parse(`{${itemsString.replace(/&quot;/g, '"').replace(/,\s*$/, '')}}`);
const generateGallery = (_: string, itemsString: string) => {
const items: Record<string, string> = JSON.parse(`{${itemsString.replace(/&quot;/g, '"').replace(/,\s*$/, '')}}`);
const label = (alt) => {
const label = (alt: string): string => {
if (!alt.startsWith('! ')) {
return '';
}
@ -84,7 +85,7 @@ const generateGallery = (_, itemsString) => {
return `<div class="row columnist-wall--disabled">${cells.join('')}</div>`;
};
export default async function parseMarkdown(markdown, translator) {
export default async function parseMarkdown(markdown: string, translator: Translator) {
let content = `<div>${
markdown
.replace(/<table>/g, '<div class="table-responsive"><table class="table table-striped small">')
@ -142,7 +143,7 @@ export default async function parseMarkdown(markdown, translator) {
const title = titleMatch ? titleMatch[1] : null;
const imgMatch = content.match('<img src="([^"]+)"[^>]*>');
const img = imgMatch ? imgMatch[1] : null;
let intro = [];
let intro: string[] = [];
for (const introMatch of content.matchAll(/<p[^>]*>(.+?)<\/p>/gms)) {
const p = introMatch[1].replace(/(<([^>]+)>)/ig, '').replace(/\s+/g, ' ');