@@ -71,7 +71,7 @@ export const mf2ToJf2 = async (body, requestReferences) => {
71
71
* @returns {object } Normalised JF2 properties
72
72
*/
73
73
export const normaliseProperties = ( publication , properties , timeZone ) => {
74
- const { me, slugSeparator } = publication ;
74
+ const { channels , me, slugSeparator } = publication ;
75
75
76
76
properties . published = getDate ( timeZone , properties . published ) ;
77
77
@@ -101,6 +101,11 @@ export const normaliseProperties = (publication, properties, timeZone) => {
101
101
102
102
properties . slug = getSlugProperty ( properties , slugSeparator ) ;
103
103
104
+ const publicationHasChannels = channels && Object . keys ( channels ) . length > 0 ;
105
+ if ( publicationHasChannels ) {
106
+ properties [ "mp-channel" ] = getChannelProperty ( properties , channels ) ;
107
+ }
108
+
104
109
if ( properties [ "mp-syndicate-to" ] ) {
105
110
properties [ "mp-syndicate-to" ] = toArray ( properties [ "mp-syndicate-to" ] ) ;
106
111
}
@@ -127,6 +132,44 @@ export const getAudioProperty = (properties, me) => {
127
132
} ) ) ;
128
133
} ;
129
134
135
+ /**
136
+ * Get channel property.
137
+ *
138
+ * If a publication has configured channels, but no channel has been selected,
139
+ * the default channel is used.
140
+ *
141
+ * If `mp-channel` provides a UID that does not appear in the publication’s
142
+ * channels, the default channel is used.
143
+ *
144
+ * The first item in a publication’s configured channels is considered the
145
+ * default channel.
146
+ * @param {object } properties - JF2 properties
147
+ * @param {object } channels - Publication channels
148
+ * @returns {Array } `mp-channel` property
149
+ * @see {@link https://github.com/indieweb/micropub-extensions/issues/40 }
150
+ */
151
+ export const getChannelProperty = ( properties , channels ) => {
152
+ channels = Object . keys ( channels ) ;
153
+ const mpChannel = properties [ "mp-channel" ] ;
154
+ const providedChannels = Array . isArray ( mpChannel ) ? mpChannel : [ mpChannel ] ;
155
+ const selectedChannels = new Set ( ) ;
156
+
157
+ // Only select channels that have been configured
158
+ for ( const uid of providedChannels ) {
159
+ if ( channels . includes ( uid ) ) {
160
+ selectedChannels . add ( uid ) ;
161
+ }
162
+ }
163
+
164
+ // If no channels provided, use default channel UID
165
+ if ( selectedChannels . size === 0 ) {
166
+ const defaultChannel = channels [ 0 ] ;
167
+ selectedChannels . add ( defaultChannel ) ;
168
+ }
169
+
170
+ return toArray ( [ ...selectedChannels ] ) ;
171
+ } ;
172
+
130
173
/**
131
174
* Get content property.
132
175
*
0 commit comments