|
| 1 | +import u from 'unist-builder' |
| 2 | +import x from 'xastscript' |
| 3 | +import bcp47 from 'bcp-47-normalize' |
| 4 | +import {toAuthor} from './util.mjs' |
| 5 | + |
| 6 | +export function atom(channel, data) { |
| 7 | + var now = new Date() |
| 8 | + var meta = channel || {} |
| 9 | + var items = [] |
| 10 | + var index = -1 |
| 11 | + var offset |
| 12 | + var children |
| 13 | + var datum |
| 14 | + var value |
| 15 | + |
| 16 | + if (meta.title == null) throw new Error('Expected `channel.title` to be set') |
| 17 | + if (meta.url == null) throw new Error('Expected `channel.url` to be set') |
| 18 | + |
| 19 | + items.push(x('title', String(meta.title))) |
| 20 | + items.push(x('subtitle', String(meta.description || ''))) |
| 21 | + value = new URL(meta.url).href |
| 22 | + // `rel: 'alternate'` is the default. |
| 23 | + items.push(x('link', value)) |
| 24 | + items.push(x('id', value)) |
| 25 | + items.push(x('updated', now.toGMTString())) |
| 26 | + |
| 27 | + if (meta.feedUrl) { |
| 28 | + items.push( |
| 29 | + x('link', { |
| 30 | + href: new URL(meta.feedUrl).href, |
| 31 | + rel: 'self', |
| 32 | + type: 'application/atom+xml' |
| 33 | + }) |
| 34 | + ) |
| 35 | + } |
| 36 | + |
| 37 | + if (meta.author) { |
| 38 | + value = toAuthor(meta.author) |
| 39 | + items.push( |
| 40 | + x('rights', '© ' + now.getUTCFullYear() + ' ' + value.name), |
| 41 | + createAuthor(value) |
| 42 | + ) |
| 43 | + } |
| 44 | + |
| 45 | + if (meta.tags) { |
| 46 | + offset = -1 |
| 47 | + while (++offset < meta.tags.length) { |
| 48 | + items.push(x('category', {term: String(meta.tags[offset])})) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + if (data) { |
| 53 | + while (++index < data.length) { |
| 54 | + datum = data[index] |
| 55 | + children = [] |
| 56 | + |
| 57 | + if (!datum.title && !datum.description && !datum.descriptionHtml) { |
| 58 | + throw new Error( |
| 59 | + 'Expected either `title` or `description` to be set in entry `' + |
| 60 | + index + |
| 61 | + '`' |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + if (datum.title) children.push(x('title', String(datum.title))) |
| 66 | + |
| 67 | + if (datum.author) { |
| 68 | + children.push(createAuthor(toAuthor(datum.author))) |
| 69 | + } else if (!meta.author) { |
| 70 | + throw new Error( |
| 71 | + 'Expected `author` to be set in entry `' + |
| 72 | + index + |
| 73 | + '` or in the channel' |
| 74 | + ) |
| 75 | + } |
| 76 | + |
| 77 | + if (datum.url) { |
| 78 | + value = new URL(datum.url).href |
| 79 | + children.push(x('link', {href: value})) |
| 80 | + children.push(x('id', value)) |
| 81 | + } |
| 82 | + |
| 83 | + value = datum.published |
| 84 | + |
| 85 | + if (value != null) { |
| 86 | + if (typeof value !== 'object') value = new Date(value) |
| 87 | + children.push(x('published', value.toISOString())) |
| 88 | + } |
| 89 | + |
| 90 | + value = datum.modified |
| 91 | + |
| 92 | + if (value != null) { |
| 93 | + if (typeof value !== 'object') value = new Date(value) |
| 94 | + children.push(x('updated', value.toISOString())) |
| 95 | + } |
| 96 | + |
| 97 | + if (datum.tags) { |
| 98 | + offset = -1 |
| 99 | + while (++offset < datum.tags.length) { |
| 100 | + items.push(x('category', {term: String(datum.tags[offset])})) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + value = datum.enclosure |
| 105 | + |
| 106 | + if (value) { |
| 107 | + if (!value.url) { |
| 108 | + throw new Error( |
| 109 | + 'Expected either `enclosure.url` to be set in entry `' + index + '`' |
| 110 | + ) |
| 111 | + } |
| 112 | + |
| 113 | + if (!value.size) { |
| 114 | + throw new Error( |
| 115 | + 'Expected either `enclosure.size` to be set in entry `' + |
| 116 | + index + |
| 117 | + '`' |
| 118 | + ) |
| 119 | + } |
| 120 | + |
| 121 | + if (!value.type) { |
| 122 | + throw new Error( |
| 123 | + 'Expected either `enclosure.type` to be set in entry `' + |
| 124 | + index + |
| 125 | + '`' |
| 126 | + ) |
| 127 | + } |
| 128 | + |
| 129 | + // Can’t use `xastscript` because of `length` |
| 130 | + children.push({ |
| 131 | + type: 'element', |
| 132 | + name: 'link', |
| 133 | + attributes: { |
| 134 | + rel: 'enclosure', |
| 135 | + href: new URL(value.url).href, |
| 136 | + length: String(value.size), |
| 137 | + type: value.type |
| 138 | + }, |
| 139 | + children: [] |
| 140 | + }) |
| 141 | + } |
| 142 | + |
| 143 | + if (datum.descriptionHtml || datum.description) { |
| 144 | + children.push( |
| 145 | + x( |
| 146 | + 'content', |
| 147 | + // `type: "text"` is the default. |
| 148 | + {type: datum.descriptionHtml ? 'html' : undefined}, |
| 149 | + String(datum.descriptionHtml || datum.description) |
| 150 | + ) |
| 151 | + ) |
| 152 | + } |
| 153 | + |
| 154 | + items.push(x('entry', children)) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return u('root', [ |
| 159 | + u('instruction', {name: 'xml'}, 'version="1.0" encoding="utf-8"'), |
| 160 | + x( |
| 161 | + 'feed', |
| 162 | + { |
| 163 | + xmlns: 'http://www.w3.org/2005/Atom', |
| 164 | + 'xml:lang': meta.lang ? bcp47(meta.lang) : undefined |
| 165 | + }, |
| 166 | + items |
| 167 | + ) |
| 168 | + ]) |
| 169 | +} |
| 170 | + |
| 171 | +function createAuthor(value) { |
| 172 | + return x('author', [ |
| 173 | + x('name', String(value.name)), |
| 174 | + value.email ? x('email', String(value.email)) : undefined, |
| 175 | + value.url ? x('uri', new URL(value.url).href) : undefined |
| 176 | + ]) |
| 177 | +} |
0 commit comments