Trello Markdown Enhancer

Enhance Trello card description markdown rendering with marked.js

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

You will need to install an extension such as Tampermonkey to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         Trello Markdown Enhancer
// @description  Enhance Trello card description markdown rendering with marked.js
// @version      0.1
// @author       Hikerpig
// @copyright    2019 hikerpig (https://openuserjs.org/users/hikerpig)
// @licence      MIT
// @match        https://trello.com/*
// @require      https://cdn.jsdelivr.net/npm/marked/marked.min.js
// @grant        none
// @namespace https://greasyfork.org/users/325041
// ==/UserScript==

;(function() {
  'use strict'

  /**
   * @type MutationObserver
   */
  let mo

  function renderContent(input) {
    let text
    if (typeof input === 'string') {
      text = input
    } else if (input instanceof Element) {
      text = input.innerHTML
    }
    return marked(text)
  }

  // function getRawMarkdown(descEle) {
  //   let rawMarkdown = descEle.innerText
  //   Object.keys(descEle).forEach(k => {
  //     if (descEle[k].unmarkeddown) {
  //       rawMarkdown = descEle[k].unmarkeddown
  //     }
  //   })
  //   return rawMarkdown
  // }

  /**
   * Enhance description rendering with marked.js
   */
  function enhanceDesc() {
    const descEle = document.querySelector('.js-desc')
    if (!descEle) return
    const pEles = descEle.querySelectorAll('p')
    pEles.forEach((pEle) => {
      const text = pEle.innerText
      let hasTable = text.split('\n').some((lineContent) => {
        if (/\|\s*---\s*|/.test(lineContent)) {
          return true
        }
      })
      if (hasTable) {
        const newHTML = marked(text)
        pEle.innerHTML = newHTML
      }
    })
  }

  function bindOverlayEvents() {
    const tabParentEle = document.querySelector('.js-tab-parent')
    if (!mo) {
      mo = new MutationObserver(mutations => {
        // console.log('mutations', mutations)
        let hasCardDetail = false
        mutations.forEach(m => {
          if (m.addedNodes.length) {
            hasCardDetail = true
          }
        })

        if (hasCardDetail) {
          enhanceDesc()
        }
      })
    }
    mo.observe(tabParentEle, { childList: true })
  }

  // window.enhanceDesc = enhanceDesc

  // ---------------- start -----------------------
  enhanceDesc()
  bindOverlayEvents()
})()