showdownjs / showdown

A bidirectional Markdown to HTML to Markdown converter written in Javascript
http://www.showdownjs.com/
MIT License
14.26k stars 1.56k forks source link

Converter HTML to MARKDOWN and then MARKDOWN to HTML with coustom tags #880

Closed prasannaupadhya closed 2 years ago

prasannaupadhya commented 2 years ago

Hi,

Thanks for this library,

I am trying to convert HTML text to MARKDOWN and save the content. If I want to edit text, then I want to convert MARKDOWN text to HTML format again.

I can do both by using makeHtml and makeMarkdown. But the problem is custom tags.

Ex: I have HTML text like: <!DOCTYPE HTML><html><body><h1 class="the-cls-name">Test One<h1><br></body></html>

When it is converted to markdown format(using makeMarkdown), the output will be: # Test One.

When I want to edit this text, then again it will convert to HTML text from MARKDOWN (using makeHtml),

But in case, I should have the same class name like class="the-cls-name" in HTML.

But what I am getting is: <h1 id="testone">Test One</h1><h1 id="nbsp">&nbsp;</h1>.

I want result like: <h1 class="the-cls-name">Test One<h1>.

Is that possible?

I am doing something like this (pseudo-code):

import showdown from "showdown";

const converter = new showdown.Converter();

const text = <!DOCTYPE HTML><html><body><h1 class="the-cls-name">Test One<h1><br></body></html>

const toMD = async (text, ...) => {
  convert(text, "md");
};

const toHTML = async (...) => {
  convert(text, "html");
  ...
};

const convert = (contents, mode) => {
  switch (mode) {
    case "html":
      return converter.makeHtml(contents);
    case "md":
      return converter.makeMarkdown(contents);
  }
};
SyntaxRules commented 2 years ago

There is no provision in the CommonMark spec or GitHub Markdown spec for handing classes in mark down. Said a different way, there is not a defined way to add class metdata to markdown. Unfortunately that means this is not something we can add to showdown. You may consider creating an extension to read a custom class syntax.

See the extension documentation here. Note that this will change in 3.0.0 and likely become easier.