Edit Markdown Table

Posted by agiletortoise, Last update almost 3 years ago

Demonstration action that uses the Table Magic Markdown table editing library to modify Markdown tables in an advanced HTML Preview.

This tools allows editing of table in forms, CSV, etc. Select an existing Markdown table in the draft before running the action to pre-populate the editor.


  • script

    // locate range and default table value
    let [st, len] = editor.getSelectedLineRange();
    let text = `| Col1 | Col2 |
    | --- | --- |
    | Val1 | Val2 |`;
    if (len > 1) {
    	text = editor.getTextInRange(st, len);
  • script

    const template = `<!DOCTYPE html>
    <html lang="en">
        <script src=""></script>
        <script src=""></script>
        <script src=""></script>
        <link rel="stylesheet" type="text/css" href="">
        <link rel="stylesheet" type="text/css"
        <link rel="stylesheet" type="text/css"
    <body onload="initialValues();">
        <div class="masthead">
            <div class="container">
                <div class="title">table-magic</div>
        <div class="container">
            <div class="tabnav">
                <nav class="tabnav-tabs">
                    <a href="#" class="tabnav-tab selected" id="tab-csv" onclick="changeTab('csv');"><span
                            class="octicon octicon-file-text"></span> CSV/TSV</a>
                    <a href="#" class="tabnav-tab" id="tab-md" onclick="changeTab('md');"><span
                            class="octicon octicon-code"></span> Markdown</a>
                    <a href="#" class="tabnav-tab" id="tab-sql" onclick="changeTab('sql');"><span
                            class="octicon octicon-database"></span> SQL</a>
                    <a href="#" class="tabnav-tab" id="tab-html" onclick="changeTab('html');"><span
                            class="octicon octicon-file-code"></span> HTML</a>
                    <a href="#" class="tabnav-tab" id="tab-form" onclick="changeTab('form');"><span
                            class="octicon octicon-pencil"></span> Form</a>
                    <a href="#" class="tabnav-tab" id="tab-preview" onclick="changeTab('preview');"><span
                            class="octicon octicon-browser"></span> Preview</a>
            <div class="form-tools">
                <div class="form-button" id="tablebuilder-start" title="Drag out a new table">
                    <span class="octicon octicon-plus"></span> New table
                <div class="form-button" id="new-row" title="Add a row to the bottom">
                    <span class="octicon octicon-triangle-down"></span> Add row
                <div class="form-button" id="new-column" title="Add a column to the right">
                    <span class="octicon octicon-triangle-right"></span> Add column
                <div class="form-button" id="toggle-delete" title="Toggle deletion of rows and columns">
                    <span class="octicon octicon-trashcan"></span> Enable deletion
        <div class="container" id="viewport">
            <div id="display"></div>
        <div class="container">
            <div id="csv-options" class="options">
                    <button class="btn" type="button" onclick="downloadCsv();"><span
                            class="octicon octicon-cloud-download"></span> Download</button>
            <div id="md-options" class="options">
                <div class="form-checkbox">
                        <input type="checkbox" checked="checked" id="check-formatter">
                        Table formatter
                    <p class="note">
                        Makes markdown more legible.
            <div id="sql-info" class="options">
                <p class="note">
                    Read about the SQL function <a href="">in this pull
            <div class="footer">
                <div style="margin: 1em; padding: 1em;">
                    <button onclick="cancel();" style='font-size: 1.5em;'>Cancel</button>&nbsp;
                    <button onclick="submit();"  style='font-size: 1.5em;color: black;'>Save Changes</button>
                built using table-magic | <a href="">repo</a>
            const initialMarkdown = \`[[markdown]]\`;
            function initialValues() {
                let array = md2array(initialMarkdown);
                let html = array2preview(array);
                array_storge = array; // Store the array
                // Update variables
                tab = 'preview';
            function submit() {
                let text = $('textarea').val();
                Drafts.send('markdown', text);
            function cancel() {
    const html = template.replace("[[markdown]]", text);
    let preview = HTMLPreview.create();
    preview.hideInterface = true;
    if ( {
    	let result = context.previewValues["markdown"];
    	if (len <= 1) {
    		result = `
    	editor.setTextInRange(st, len, result);
    	editor.setSelectedRange(st, result.length);
    else {


  • After Success Default
    Notification Error
    Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.