Skip to content

Commit c2c6221

Browse files
committed
Remove reduce in BlogApi RSS XML building
1 parent b18a61e commit c2c6221

File tree

2 files changed

+34
-33
lines changed

2 files changed

+34
-33
lines changed

src/common/BlogApi.mjs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ function toXmlString(siteTitleOpt, siteDescriptionOpt, items) {
8282
var latestPubDateStr = dateToUTCString(item.pubDate);
8383
return "<lastBuildDate>" + latestPubDateStr + "</lastBuildDate>";
8484
})), "");
85-
var itemsStr = Belt_Array.reduce(items, "", (function (acc, item) {
86-
var description = item.description;
87-
var href = item.href;
88-
var descriptionElement = description === "" ? "" : "<description>\n <![CDATA[" + description + "]]>\n </description>";
89-
var dateStr = dateToUTCString(item.pubDate);
90-
return acc + ("\n <item>\n <title> <![CDATA[" + item.title + "]]></title>\n <link> " + href + " </link>\n <guid> " + href + " </guid>\n " + descriptionElement + "\n <pubDate>" + dateStr + "</pubDate>\n </item>");
91-
}));
92-
return "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n <rss version=\"2.0\">\n <channel>\n <title>" + siteTitle + "</title>\n <link>https://rescript-lang.org</link>\n <description>" + siteDescription + "</description>\n <language>en</language>\n " + latestPubDateElement + "\n" + itemsStr + "\n </channel>\n </rss>";
85+
var itemsStr = items.map(function (param) {
86+
var description = param.description;
87+
var href = param.href;
88+
var descriptionElement = description === "" ? "" : "<description>\n <![CDATA[" + description + "]]>\n </description>";
89+
var dateStr = dateToUTCString(param.pubDate);
90+
return "\n <item>\n <title> <![CDATA[" + param.title + "]]></title>\n <link> " + href + " </link>\n <guid> " + href + " </guid>\n " + descriptionElement + "\n <pubDate>" + dateStr + "</pubDate>\n </item>";
91+
}).join("\n");
92+
return "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n <rss version=\"2.0\">\n <channel>\n <title>" + siteTitle + "</title>\n <link>https://rescript-lang.org</link>\n <description>" + siteDescription + "</description>\n <language>en</language>\n " + latestPubDateElement + "\n" + itemsStr + "\n </channel>\n </rss>";
9393
}
9494

9595
var RssFeed = {

src/common/BlogApi.res

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -138,36 +138,37 @@ module RssFeed = {
138138
})
139139
->Belt.Option.getWithDefault("")
140140

141-
let itemsStr = Belt.Array.reduce(items, "", (acc, item) => {
142-
let {title, pubDate, description, href} = item
143-
144-
let descriptionElement = switch description {
145-
| "" => ""
146-
| desc => j`<description>
147-
<![CDATA[$desc]]>
148-
</description>`
149-
}
141+
let itemsStr =
142+
items
143+
->Js.Array2.map(({title, pubDate, description, href}) => {
144+
let descriptionElement = switch description {
145+
| "" => ""
146+
| desc => j`<description>
147+
<![CDATA[$desc]]>
148+
</description>`
149+
}
150150

151-
// TODO: convert pubdate to string
152-
let dateStr = pubDate->dateToUTCString
153-
j`${acc}
154-
<item>
155-
<title> <![CDATA[$title]]></title>
156-
<link> $href </link>
157-
<guid> $href </guid>
158-
$descriptionElement
159-
<pubDate>$dateStr</pubDate>
160-
</item>`
161-
})
151+
// TODO: convert pubdate to string
152+
let dateStr = pubDate->dateToUTCString
153+
j`
154+
<item>
155+
<title> <![CDATA[$title]]></title>
156+
<link> $href </link>
157+
<guid> $href </guid>
158+
$descriptionElement
159+
<pubDate>$dateStr</pubDate>
160+
</item>`
161+
})
162+
->Js.Array2.joinWith("\n")
162163

163164
let ret = j`<?xml version="1.0" encoding="utf-8" ?>
164165
<rss version="2.0">
165166
<channel>
166-
<title>$siteTitle</title>
167-
<link>https://rescript-lang.org</link>
168-
<description>$siteDescription</description>
169-
<language>en</language>
170-
$latestPubDateElement
167+
<title>$siteTitle</title>
168+
<link>https://rescript-lang.org</link>
169+
<description>$siteDescription</description>
170+
<language>en</language>
171+
$latestPubDateElement
171172
$itemsStr
172173
</channel>
173174
</rss>` //rescript-lang.org</link>

0 commit comments

Comments
 (0)