Small javascript library for manipulating markdown tables
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.
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.
table
is a string containing the modified table.cursor
is a {line, ch}
object that indicates cursor coordinates within table
.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:
rows
is the number of rows added to an empty table when not specified (default is 3
)cols
is the number of columns added to an empty table when not specified (default is 2
)true
, extraPipes
wraps each line of the table into optional pipes.true
, extraSpaces
adds extra spaces around each cell's of the table (default is true
)autoBeautify
is set to true
, table is beautified on table.get()
(default is true
)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);
.align(colIndex)
Returns alignment of the colIndex-th column, one of "left"
, "center"
, "right"
or undefined
, when alignment is not defined (no colons in the second row).
When not defined colIndex
is set to cursor position if cursor exists (otherwise function returns null
and Invalid colIndex
error is printed out to the console).
.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 | FooFoo | Bar |
| Foo | Bar | This is the longest cell! |
eachRow(callback)
, with callback = function (array, row, rowIndex)
eachCell(callback)
, with callback = 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.
$ npm init
src/celldown.coffee
$ grunt
Some ideas:
null
as a coordinate for representing the last item of a collection (line, row, cell...){line, ch}
and {col, row, ch}
coordinates. Actually it already exists in txt2arr()
and cursor.get()
methods for handling cursor coord conversion, so we could possibly get inspired..addContent(arr, coord)
method for filling with content
a set of cells defined in coord
.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.