celldown.js is a small javascript library for manipulating markdown tables.
It provides several very basic features for handling markdown (GFM) tables in a web-based application.
- Creating markdown tables from text or from coordinates
- Adding and removing cols and rows
- Setting columns alignment
- Improving format (beautify)
- Tracking the cursor position while doing those operations
Use it as a Node.js module :
$ npm install celldown
then :
var celldown = require("celldown");...Or directly in the browser :
$ bower install celldown
and :
<script src="path_to_script/celldown.min.js">First, create a new table from a string :
var myTable = celldown.fromText(str);Or create an empty table :
// 3 cols and 5 rows
var myTable = celldown.empty(3, 5);Then do stuff:
var editedTable = myTable.addRow(3).beautify().get().text;
console.log(editedTable);If you need to track a cursor while the table is edited, add it as a {line, ch} object in the last parameter :
var myCursor = {
line: 2,
ch: 10
};
var myTable = celldown.fromText(str, cursor);table.get() method returns a {table, cursor} object.
tableis a string containing the modified table.cursoris a{line, ch}object that indicates cursor coordinates withintable.
Example:
var t = table.get();
console.log("Modified table is:\n" + t.table + "\n...and cursor position is line: " + t.cursor.line + ", ch: " + t.cursor.ch);Configuration is stored in celldown.config and can be modified with celldown.setConfig().
The following are available:
rowsis the number of rows added to an empty table when not specified (default is3)colsis the number of columns added to an empty table when not specified (default is2)- If set to
true,extraPipeswraps each line of the table into optional pipes. - If set to
true,extraSpacesadds extra spaces around each cell's of the table (default istrue) - If
autoBeautifyis set totrue, table is beautified ontable.get()(default istrue)
Use celldown.setConfig(newConfigObj) to change defaults:
celldown.setConfig({
rows: 2,
autoBeautify: false
});.addRows(index, numberOfAdditions).addCols(index, numberOfAdditions)addRowsBeforeCursor(numberOfAdditions)addRowsAfterCursor(numberOfAdditions)addColsBeforeCursor(numberOfAdditions)addColsAfterCursor(numberOfAdditions)
When not defined index is set to cursor position if cursor exists (otherwise it is set to 0).
When not defined numberOfAdditions is set to 1.
// Adding a row before the 4th row
table.addRows(3);
// Adding two cols at the first position
table.addCols(0, 2);.removeRows(index, numberOfDeletions).removeCols(index, numberOfDeletions)
When not defined index is set to cursor position if cursor exists (otherwise function is not executed).
When not defined numberOfAdditions is set to 1.
// Removing the third col
table.removeCols(2);
// Removing 4 rows from the 3th
table.removeRows(2, 4);.align(colIndex, side)
When not defined colIndex is set to cursor position if cursor exists (otherwise function is not executed).
When side is not defined, column alignment is cleared.
// Align columns 0, 1 and 2
table.align(0, "left");
table.align(1, "center");
table.align(3, "right");
// Clear align of col 0
table.align(0);.beautify()
Beautify the table. It transforms this:
|I'm a table |generated |with celldown.js|
|--|--|------------------------------------------------------------ |
|Hello World| Foo | Bar |
|Foo|Bar|This is the longest cell!|
into this:
| I'm a table | generated | with celldown.js |
| ----------- | --------- | ------------------------- |
| Hello World | Foo | Bar |
| Foo | Bar | This is the longest cell! |
eachRow(callback), withcallback = function (array, row, rowIndex)eachCell(callback), withcallback = function (array, cell, rowIndex, colIndex)
getSize()
Returns a {cols, rows} object that contains the length of cols and rows.
celldown.isValidTable(str) returns true if str is recognized as a valid GFM table. Please view celldown.js source for more information.
Contributions are welcome.
- Fork repo
- Init project:
$ npm init - Modify source in
src/celldown.coffee - Build project:
$ grunt - Pull request
Some ideas:
- Support
nullas a coordinate for representing the last item of a collection (line, row, cell...) - Add methods to perform tranlation between
{line, ch}and{col, row, ch}coordinates. Actually it already exists intxt2arr()andcursor.get()methods for handling cursor coord conversion, so we could possibly get inspired. - Add a
.addContent(arr, coord)method for filling withcontenta set of cells defined incoord. - ...?
The MIT License (MIT)
Copyright (c) 2015 Thomas Brouard
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.