Atlassian Document Format Textarea JSON to Plain Text
by averylane95
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSON to Text</title>
</head>
<body>
<h1>Atlassian Document Format Textarea JSON to Plain Text</h1>
<form id="json-form">
<label for="json-input">Enter JSON:</label><br>
<textarea id="json-input" rows="10" cols="80"></textarea><br>
<button type="submit">Extract Text</button>
</form>
<h2>Extracted Text:</h2>
<pre id="result"></pre>
</body>
</html>
CSS
body {
font-family: Arial, sans-serif;
background-color: #f5f5f5;
margin: 0;
padding: 0;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: white;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
border-radius: 5px;
}
h1 {
font-size: 24px;
font-weight: bold;
margin-bottom: 20px;
}
label {
display: block;
font-size: 16px;
font-weight: bold;
margin-bottom: 5px;
}
textarea {
display: block;
width: 100%;
font-family: inherit;
font-size: 14px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
resize: vertical;
}
button {
display: inline-block;
background-color: #3a8de3;
color: white;
font-size: 16px;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
margin-top: 10px;
}
button:hover {
background-color: #2172c1;
}
h2 {
font-size: 18px;
font-weight: bold;
margin-bottom: 10px;
}
pre {
background-color: #f5f5f5;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
white-space: pre-wrap;
font-size: 14px;
}
JavaScript
// 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;
}
// Get form and result elements
const jsonForm = document.getElementById("json-form");
const result = document.getElementById("result");
// Add event listener to the form
jsonForm.addEventListener("submit", (event) => {
event.preventDefault();
const jsonInput = document.getElementById("json-input").value;
try {
const parsedJSON = JSON.parse(jsonInput);
const extractedText = extractTextFromJSON(parsedJSON);
result.innerText = extractedText;
} catch (error) {
result.innerText = "Invalid JSON input. Please check your input and try again.";
}
});