(ts) migrate src/parseMarkdown.ts

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

View File

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

View File

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

View File

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

View File

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