editor.js icon indicating copy to clipboard operation
editor.js copied to clipboard

Render to plain text

Open johnwood-wff opened this issue 6 years ago • 4 comments

It would be nice if there was an easy way to just render the whole thing to plain text, especially for text-to-speech, or performing diffs. I know there are ways to strip out tags from the rendered HTML, but given there's more metadata in the clean json it might make sense to provide an API way of retrieving this more accurately.

EDIT: Or how about extending the API so that each plugin can render to different formats, and have HTML, plain text, markdown, or even MediaWiki as render options?

johnwood-wff avatar May 06 '19 17:05 johnwood-wff

The rendering of a data is not at the Editor's area of responsibility. You are free to render JSON as you want.

We are thinking about some API for Tools that will allow to provide any rendering formats. But there are many questions yet.

neSpecc avatar Jun 11 '19 12:06 neSpecc

Why is this repo tagged with wysiwyg then? As i need the rendering, i'd be willing to contribute to some rendering API.

But there are many questions yet.

Could you specify the Questions and what challenges there are to take?

marvinside avatar Dec 21 '19 16:12 marvinside

Related #742

neSpecc avatar Dec 26 '19 13:12 neSpecc

It would ideal if all editorjs tools used "text" field name, but here's a start. Just add more block types, etc. to the filter as we discover them...


function extractTextContent(data) {
  const results = [];

  function recursivelyExtract(obj) {
    if (typeof obj === 'object' && obj !== null) {
      if (Array.isArray(obj)) {
        obj.forEach(item => recursivelyExtract(item));
      } else {
        for (const key in obj) {
          if (key === 'text' || key === 'content' && typeof obj[key] === 'string') {
            results.push(obj[key]);
          } else {
            recursivelyExtract(obj[key]);
          }
        }
      }
    }
  }

  recursivelyExtract(data);
  return results;
}

// Example usage:
const jsonString = `{
  "article": {
    "title": "Example Article",
    "sections": [
      {
        "heading": "Introduction",
        "content": "This is the introduction text."
      },
      {
        "heading": "Body",
        "paragraphs": [
          {
            "text": "First paragraph text."
          },
          {
            "sub_section":{
                "text": "nested text"
            }
          },
          {
            "content": "second paragraph content"
          }
        ]
      }
    ]
  },
  "metadata": {
    "author": "John Doe",
    "summary": {
      "text": "A brief summary."
    }
  }
}`;

const data = JSON.parse(jsonString);
const extractedTexts = extractTextContent(data);
console.log(extractedTexts);

chadsteele avatar Mar 30 '25 15:03 chadsteele