Add types

This commit is contained in:
Sidharth Vinod 2023-06-13 11:26:25 +05:30
parent 5903792207
commit dd4e14690d
No known key found for this signature in database
GPG Key ID: FB5CCD378D3907CD

View File

@ -1,11 +1,12 @@
import type { Content } from 'mdast';
import { fromMarkdown } from 'mdast-util-from-markdown'; import { fromMarkdown } from 'mdast-util-from-markdown';
import { dedent } from 'ts-dedent'; import { dedent } from 'ts-dedent';
/** /**
* @param {string} markdown markdown to process * @param markdown - markdown to process
* @returns {string} processed markdown * @returns processed markdown
*/ */
function preprocessMarkdown(markdown) { function preprocessMarkdown(markdown: string): string {
// Replace multiple newlines with a single newline // Replace multiple newlines with a single newline
const withoutMultipleNewlines = markdown.replace(/\n{2,}/g, '\n'); const withoutMultipleNewlines = markdown.replace(/\n{2,}/g, '\n');
// Remove extra spaces at the beginning of each line // Remove extra spaces at the beginning of each line
@ -14,19 +15,15 @@ function preprocessMarkdown(markdown) {
} }
/** /**
* @param {string} markdown markdown to split into lines * @param markdown - markdown to split into lines
*/ */
export function markdownToLines(markdown) { export function markdownToLines(markdown: string) {
const preprocessedMarkdown = preprocessMarkdown(markdown); const preprocessedMarkdown = preprocessMarkdown(markdown);
const { children } = fromMarkdown(preprocessedMarkdown); const { children } = fromMarkdown(preprocessedMarkdown);
const lines = [[]]; const lines: { content: string; type: string }[][] = [[]];
let currentLine = 0; let currentLine = 0;
/** function processNode(node: Content, parentType = 'normal') {
* @param {import('mdast').Content} node
* @param {string} [parentType]
*/
function processNode(node, parentType = 'normal') {
if (node.type === 'text') { if (node.type === 'text') {
const textLines = node.value.split('\n'); const textLines = node.value.split('\n');
textLines.forEach((textLine, index) => { textLines.forEach((textLine, index) => {
@ -58,17 +55,10 @@ export function markdownToLines(markdown) {
return lines; return lines;
} }
/** export function markdownToHTML(markdown: string) {
* @param {string} markdown markdown to convert to HTML
* @returns {string} HTML
*/
export function markdownToHTML(markdown) {
const { children } = fromMarkdown(markdown); const { children } = fromMarkdown(markdown);
/** function output(node: Content): string {
* @param {import('mdast').Content} node
*/
function output(node) {
if (node.type === 'text') { if (node.type === 'text') {
return node.value.replace(/\n/g, '<br/>'); return node.value.replace(/\n/g, '<br/>');
} else if (node.type === 'strong') { } else if (node.type === 'strong') {