Public knowledge base

Here is the code that does it. It’s written in nodejs using the coda-js library.

The code loads a document named FAQ and uses one of the sections to extract all tables and its rows. I’ve added some comments to make it easier to understand, modify and use.

/**
 * This code loads a document named FAQ and uses one of the sections to extract all tables and its rows.
 */
const Coda = require('coda-js');
const marked = require('marked'); // This is a library to transform markdown to html
const coda = new Coda('CODA_API_TOKEN');

coda.listDocs({ query: 'FAQ' }) // Search documents matching name "FAQ"
  .then(docs => {
    // From all returned documents, which maybe is just one, take the one that we want. So we specify the ID
    const document = docs.find(document => document.id === 'XXXXXX'); // FAQ document ID
    return document.listTables(); // We list all tables in the document
  })
  .then(tables => {
    // Now we get back all the tables in the document, but we only want the ones in one of the sections (this is optional as I didn't want the ones for the other sections)
    const promises = tables
      .filter(table => table.parent.id === 'canvas-XXXXX') // So we filter the section that we need
      .map(table => new Promise(resolve => {
        // Here for all the tables, we list all rows and we save them in the table object as .rows
        table.listRows({
          useColumnNames: true,
          sortBy: 'natural',
          valueFormat: 'rich',
        }).then(rows => {
          table.rows = rows;
          resolve(table);
        });
      }));
    return Promise.all(promises);
  })
  .then(tables => {
    // This last step is optional, I'm doing some code manipulations
    // We have now an array of tables and each table has a .rows property with all the rows
    tables.forEach(table => {
      table.rows.forEach(row => {
        // Here I manipulate some of the values. Basically I have 2 columns in the table. Question and Answer
        // Somehow Coda adds some weird characters at the beginning and end, so I'm cleaning them here
        row.values.Question = row.values.Question.replace(/^```|```$/g, '');
        row.values.Answer = marked(row.values.Answer.replace(/^```|```$/g, ''));
        // Finally I store the row ID in the values, just to make it easier for me later to use
        row.values.id = row.id;
      });
    });
    return tables;
  })
  .catch(error => console.error(error));

Finally you have to get the returned tables and rows from the last step and print them however you want on your website.

1 Like