export function isAdfDoc(value) {
return (
value !== null &&
typeof value === 'object' &&
!Array.isArray(value) &&
value.type === 'doc' &&
value.version === 1 &&
Array.isArray(value.content)
)
}
/**
* Convert plain text (or an existing ADF doc) into Jira Cloud ADF.
* Multi-line strings become one paragraph per line.
*/
export function toAdf(textOrDoc) {
if (isAdfDoc(textOrDoc)) return textOrDoc
if (typeof textOrDoc !== 'string') {
throw new Error('Jira description/comment body must be a string or an ADF document.')
}
const lines = textOrDoc.replace(/\r\n/g, '\n').split('\n')
const content = lines.map((line) => ({
type: 'paragraph',
content: line ? [{ type: 'text', text: line }] : [],
}))
return {
type: 'doc',
version: 1,
content: content.length > 0 ? content : [{ type: 'paragraph', content: [] }],
}
}
export function adfToPlainText(doc) {
if (typeof doc === 'string') return doc
if (!isAdfDoc(doc)) return null
const parts = []
const walk = (node) => {
if (!node || typeof node !== 'object') return
if (node.type === 'text' && typeof node.text === 'string') parts.push(node.text)
if (node.type === 'hardBreak') parts.push('\n')
if (Array.isArray(node.content)) {
for (const child of node.content) walk(child)
if (node.type === 'paragraph') parts.push('\n')
}
}
walk(doc)
return parts.join('').replace(/\n+$/, '')
}