Back to JS with jsdoc types

This commit is contained in:
Sidharth Vinod
2023-04-26 11:00:35 +05:30
parent eba3a7bdcc
commit 91f1ef9c42

View File

@@ -1,8 +1,11 @@
import { fromMarkdown } from 'mdast-util-from-markdown'; import { fromMarkdown } from 'mdast-util-from-markdown';
import type { Content } from 'mdast';
import { dedent } from 'ts-dedent'; import { dedent } from 'ts-dedent';
function preprocessMarkdown(markdown: string): string { /**
* @param {string} markdown markdown to process
* @returns {string} processed markdown
*/
function preprocessMarkdown(markdown) {
// 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
@@ -10,20 +13,20 @@ function preprocessMarkdown(markdown: string): string {
return withoutExtraSpaces; return withoutExtraSpaces;
} }
interface Word { /**
content: string; * @param {string} markdown markdown to split into lines
type: string; */
} export function markdownToLines(markdown) {
type Line = Word[];
export function markdownToLines(markdown: string): Line[] {
const preprocessedMarkdown = preprocessMarkdown(markdown); const preprocessedMarkdown = preprocessMarkdown(markdown);
const { children } = fromMarkdown(preprocessedMarkdown); const { children } = fromMarkdown(preprocessedMarkdown);
const lines: Line[] = [[]]; const lines = [[]];
let currentLine = 0; let currentLine = 0;
function processNode(node: Content, parentType?: string) { /**
* @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) => {
@@ -33,7 +36,7 @@ export function markdownToLines(markdown: string): Line[] {
} }
textLine.split(' ').forEach((word) => { textLine.split(' ').forEach((word) => {
if (word) { if (word) {
lines[currentLine].push({ content: word, type: parentType || 'normal' }); lines[currentLine].push({ content: word, type: parentType });
} }
}); });
}); });
@@ -55,10 +58,17 @@ export function markdownToLines(markdown: string): Line[] {
return lines; return lines;
} }
export function markdownToHTML(markdown: string): 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') {