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

handling other medium extensions #31

Open
joeblew99 opened this issue Sep 5, 2016 · 3 comments
Open

handling other medium extensions #31

joeblew99 opened this issue Sep 5, 2016 · 3 comments

Comments

@joeblew99
Copy link

hey everyone,
never seen a html to markdown component. normally its the other way around.

i am wondering if you have thought about how to save into the markdown the other potential medium extensions.
For example one extension i have seen makes tables (yes we can save that in markdown).
But another allows positioning in temrs of left or right justification. I dont think markdown is capable of holding that sort of data.

wondering what you think

cheers

@IonicaBizau
Copy link
Owner

Hmm, that's a good point, indeed. Ideas are welcome.

@jerrywham
Copy link

For tables, see #53 . It's pretty simple to do.
Add this code in me-mardown.standalone.js (start at line 157) allows to do it :

// ######################################
// #                                    #
// # ██████  ████  █████  ████   ██████ #
// # █ ██ █ ██  ██ ██  ██  ██    ██     #
// #   ██   ██  ██ ██  ██  ██    ██     #
// #   ██   ██████ █████   ██    █████  #
// #   ██   ██  ██ ██  ██  ██    ██     #
// #   ██   ██  ██ ██  ██  ██ ██ ██     #
// #  ████  ██  ██ █████  ██████ ██████ #
// #                                    #
// ######################################
var NL = "\n";

function convertTableElementToMarkdown(tableEl) {
  var rows = [];
  var trEls = tableEl.getElementsByTagName('tr');
  for(var i=0; i<trEls.length; i++) {
    var tableRow = trEls[i];
    var markdownRow = convertTableRowElementToMarkdown(tableRow, i);
    rows.push(markdownRow);
  }
  return rows.join(NL);
}

function convertTableRowElementToMarkdown(tableRowEl, rowNumber) {
  var cells = [];
  var cellEls = tableRowEl.children;
  for(var i=0; i<cellEls.length; i++) {
    var cell = cellEls[i];
    cells.push(cell.innerText + ' |');
  }
  var row = '| ' + cells.join(" ");

  if(rowNumber == 0) {
    row = row + NL + createMarkdownDividerRow(cellEls.length);
  }

  return row;
}

function createMarkdownDividerRow(cellCount) {
  var dividerCells = [];
  for(i = 0; i<cellCount; i++) {
    dividerCells.push('---' + ' |');
  }
  return '| ' + dividerCells.join(" ");
}
// ######################################
// #                                    #
// # ██████  ████  █████  ████   ██████ #
// # █ ██ █ ██  ██ ██  ██  ██    ██     #
// #   ██   ██  ██ ██  ██  ██    ██     #
// #   ██   ██████ █████   ██    █████  #
// #   ██   ██  ██ ██  ██  ██    ██     #
// #   ██   ██  ██ ██  ██  ██ ██ ██     #
// #  ████  ██  ██ █████  ██████ ██████ #
// #                                    #
// ######################################

/*
 * Finds a Markdown converter, gets the replacement, and sets it on
 * `_replacement`
 */

function process (node) {
  var replacement
  var content = getContent(node)

  // Remove blank nodes
  if (!isVoid(node) && !/A|TH|TD/.test(node.nodeName) && /^\s*$/i.test(content)) {
    node._replacement = ''
    return
  }

  for (var i = 0; i < converters.length; i++) {
    var converter = converters[i]

    if (canConvert(node, converter.filter)) {
      if (typeof converter.replacement !== 'function') {
        throw new TypeError(
          '`replacement` needs to be a function that returns a string'
        )
      }

      var whitespace = flankingWhitespace(node, content)

      if (whitespace.leading || whitespace.trailing) {
        content = content.trim()
      }
      replacement = whitespace.leading +
        converter.replacement.call(toMarkdown, content, node) +
        whitespace.trailing
      break
    }
  }

  if (node.nodeName == 'TABLE') {
    replacement = convertTableElementToMarkdown(node);
  }
  
  node._replacement = replacement
}

For alignement, I'm still searching

@jywarren
Copy link

The Woofmark editor (not very maintained) uses Domador (https://github.com/bevacqua/domador) to do this. It works all right, but has some bugs.

@ct1735x ct1735x mentioned this issue Jun 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants