Is it posible to get the body comment as plain text

I wrote a Javascript example script that parses the Atlassian Document Format (ADF) JSON and extracts the plain text from the “text” property of all the document nodes. It recursively iterates through all content and extracts the “text” property if it exists, and concatenates it to a string. If the content has a “content” property, it will recurse through it to extract the “text” property. Text that belongs under a common node such as paragraph are concatenated inline. Each collection of text from a node is put on a new line:

Script:

// Sample JSON
const json = {
  "version": 1,
  "type": "doc",
  "content": [
    // ...
  ]
};

// Function to extract text from JSON
function extractTextFromJSON(json) {
  let text = "";

  // Keep track of the previous node type
  let previousNodeType = null;

  // Iterate through all content
  json.content.forEach((content) => {
    // If the content has "text" property, append it to the string with or without a newline character
    if (content.hasOwnProperty("text")) {
      // Check if the current node type is the same as the previous node type
      if (content.type === previousNodeType) {
        text += content.text;
      } else {
        text += "\n" + content.text;
      }

      // Update the previous node type
      previousNodeType = content.type;
    }
    // If the content has "content" property, recurse through it
    if (content.hasOwnProperty("content")) {
      text += extractTextFromJSON(content);
    }
  });

  return text;
}

// Call the function with the sample JSON
const result = extractTextFromJSON(json);

console.log(result); // Output the concatenated string with each "text" field on a new line


Example Rendered Text:
This is a rendered rich text area that I created using the Atlassian Document Builder Tool

image

JSON Object in Atlassian Document Format:

{
  "version": 1,
  "type": "doc",
  "content": [
    {
      "type": "heading",
      "attrs": {
        "level": 1
      },
      "content": [
        {
          "type": "text",
          "text": "Lorem"
        }
      ]
    },
    {
      "type": "paragraph",
      "content": [
        {
          "type": "text",
          "text": "Lorem "
        },
        {
          "type": "text",
          "text": "ipsum",
          "marks": [
            {
              "type": "strong"
            }
          ]
        },
        {
          "type": "text",
          "text": " dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris "
        },
        {
          "type": "text",
          "text": "nisi ut aliquip ex ea commodo",
          "marks": [
            {
              "type": "em"
            },
            {
              "type": "underline"
            }
          ]
        },
        {
          "type": "text",
          "text": " consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. "
        }
      ]
    },
    {
      "type": "paragraph",
      "content": [
        {
          "type": "text",
          "text": "Excepteur sint occaecat cupidatat non proident, "
        },
        {
          "type": "text",
          "text": "sunt in culpa qui officia",
          "marks": [
            {
              "type": "em"
            }
          ]
        },
        {
          "type": "text",
          "text": " deserunt mollit anim id est laborum."
        }
      ]
    },
    {
      "type": "bulletList",
      "content": [
        {
          "type": "listItem",
          "content": [
            {
              "type": "paragraph",
              "content": [
                {
                  "type": "text",
                  "text": "Sed "
                }
              ]
            }
          ]
        },
        {
          "type": "listItem",
          "content": [
            {
              "type": "paragraph",
              "content": [
                {
                  "type": "text",
                  "text": "ut "
                },
                {
                  "type": "text",
                  "text": "unde",
                  "marks": [
                    {
                      "type": "strong"
                    },
                    {
                      "type": "textColor",
                      "attrs": {
                        "color": "#bf2600"
                      }
                    }
                  ]
                }
              ]
            }
          ]
        },
        {
          "type": "listItem",
          "content": [
            {
              "type": "paragraph",
              "content": [
                {
                  "type": "text",
                  "text": "perspiciatis "
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "type": "paragraph",
      "content": []
    },
    {
      "type": "paragraph",
      "content": []
    }
  ]
}

Script Output:

Lorem
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Sed
ut unde
perspiciatis

JSFiddle Link: Atlassian Document Format Textarea JSON to Plain Text - JSFiddle - Code Playground

4 Likes