Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML support #290

Closed
lauridskern opened this issue Apr 27, 2023 · 1 comment
Closed

HTML support #290

lauridskern opened this issue Apr 27, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@lauridskern
Copy link

Is it possible to add HTML parsing?

@gmsgowtham
Copy link
Owner

gmsgowtham commented Apr 28, 2023

@lauridskern No, its not. Marked doesn't support it.

You could try converting the HTML to Markdown using Turndown and domino.

import TurndownService from "turndown";
import Domino from "domino";

TurndownService.prototype.escape = (string) => {
	// Disables string escaping
	// ref: https://github.com/mixmark-io/turndown#overriding-turndownserviceprototypeescape
	return string;
};

const turndownService = new TurndownService();

export const preProcessMd = (md: string) => {
	// Check if markdown contains html tags, if found transform them to markdown
	// This function has caveat, it'll return true if a codeblock contains some html
	if (/<\/?[a-z][\s\S]*>/gim.test(md)) {
		const mdWithBR = md.replace(/(?:\r\n|\r|\n)/gm, "<br/>"); // to preserve new lines
		const document = Domino.createDocument(mdWithBR, true);
		return turndownService
			.turndown(document)
			.trim()
			.replace(/[^\S\r\n]+$/gm, ""); // Replaces " \n" with "\n", side effect of conversion 
	}
};

I'll see how it can be integrated with the library.

@gmsgowtham gmsgowtham added the enhancement New feature or request label Apr 28, 2023
@gmsgowtham gmsgowtham pinned this issue Apr 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants