Skip to content

Commit 87b6d1b

Browse files
committed
Introduced optional includeMetadata parameter
The `includeMetadata` parameter—which defaults to `false`—allows the inclusion of extended metadata such as (a limited set of) topic attributes as well as topic relationships to be included in the sitemap. These were previously supported as they could be used by Google Custom Search Engine (CSE) in order to provide site-specific queries. While that was a really useful feature, CSE is no longer supported by Google, and thus this functionality is no longer used. (Technically, this is a breaking change. As no APIs that implementers are using currently support or rely on this, however, we're not worried about that change.) As other agents or search engines may, in the future, offer support for extended attributes via the `<PageMap />` and `<DataObject />` elements, I've kept that functionality in via the optional `includeMetadata` parameter, thus allowing this to still be used if needed. The immediate affect of this is to (dramatically) reduce the size of the default `/Sitemap` route, since it wil no longer include metadata for attributes and relationships on every topic.
1 parent 66c53c9 commit 87b6d1b

1 file changed

Lines changed: 18 additions & 11 deletions

File tree

OnTopic.AspNetCore.Mvc/Controllers/SitemapController.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ public SitemapController(ITopicRepository topicRepository) {
9191
/// Provides the Sitemap.org sitemap for the site.
9292
/// </summary>
9393
/// <param name="indent">Optionally enables indentation of XML elements in output for human readability.</param>
94-
/// <returns>The site's homepage view.</returns>
95-
public virtual ActionResult Index(bool indent = false) {
94+
/// <param name="includeMetadata">Optionally enables extended metadata associated with each topic.</param>
95+
/// <returns>A Sitemap.org sitemap.</returns>
96+
public virtual ActionResult Index(bool indent = false, bool includeMetadata = false) {
9697

9798
/*------------------------------------------------------------------------------------------------------------------------
9899
| Ensure topics are loaded
@@ -109,7 +110,7 @@ public virtual ActionResult Index(bool indent = false) {
109110
| Establish sitemap
110111
\-----------------------------------------------------------------------------------------------------------------------*/
111112
var declaration = new XDeclaration("1.0", "utf-8", "no");
112-
var sitemap = GenerateSitemap(rootTopic);
113+
var sitemap = GenerateSitemap(rootTopic, includeMetadata);
113114
var settings = indent? SaveOptions.None : SaveOptions.DisableFormatting;
114115

115116
/*------------------------------------------------------------------------------------------------------------------------
@@ -125,13 +126,15 @@ public virtual ActionResult Index(bool indent = false) {
125126
/// <summary>
126127
/// Given a root topic, generates an XML-formatted sitemap.
127128
/// </summary>
128-
/// <returns>The site's homepage view.</returns>
129+
/// <param name="topic">The topic to add to the sitemap.</param>
130+
/// <param name="includeMetadata">Optionally enables extended metadata associated with each topic.</param>
131+
/// <returns>A Sitemap.org sitemap.</returns>
129132
[Obsolete("The GenerateSitemap() method should not be public. It will be marked private in OnTopic Library 5.0.")]
130-
public virtual XDocument GenerateSitemap(Topic rootTopic) =>
133+
public virtual XDocument GenerateSitemap(Topic rootTopic, bool includeMetadata = false) =>
131134
new XDocument(
132135
new XElement(_sitemapNamespace + "urlset",
133136
from topic in rootTopic?.Children
134-
select AddTopic(topic)
137+
select AddTopic(topic, includeMetadata)
135138
)
136139
);
137140

@@ -141,8 +144,10 @@ select AddTopic(topic)
141144
/// <summary>
142145
/// Given a <see cref="Topic"/>, adds it to a given <see cref="XmlNode"/>.
143146
/// </summary>
147+
/// <param name="topic">The topic to add to the sitemap.</param>
148+
/// <param name="includeMetadata">Optionally enables extended metadata associated with each topic.</param>
144149
[Obsolete("The AddTopic() method should not be public. It will be marked private in OnTopic Library 5.0.")]
145-
public IEnumerable<XElement> AddTopic(Topic topic) {
150+
public IEnumerable<XElement> AddTopic(Topic topic, bool includeMetadata = false) {
146151

147152
/*------------------------------------------------------------------------------------------------------------------------
148153
| Establish return collection
@@ -171,21 +176,23 @@ public IEnumerable<XElement> AddTopic(Topic topic) {
171176
new XElement(_sitemapNamespace + "changefreq", "monthly"),
172177
new XElement(_sitemapNamespace + "lastmod", lastModified.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)),
173178
new XElement(_sitemapNamespace + "priority", 1),
174-
new XElement(_pagemapNamespace + "PageMap",
179+
includeMetadata? new XElement(_pagemapNamespace + "PageMap",
175180
new XElement(_pagemapNamespace + "DataObject",
176181
new XAttribute("type", topic.ContentType?? "Page"),
177182
getAttributes()
178183
),
179184
getRelationships()
180-
)
185+
) : null
181186
);
182-
topics.Add(topicElement);
187+
if (!topic.ContentType!.Equals("Container", StringComparison.InvariantCultureIgnoreCase)) {
188+
topics.Add(topicElement);
189+
}
183190

184191
/*------------------------------------------------------------------------------------------------------------------------
185192
| Iterate over children
186193
\-----------------------------------------------------------------------------------------------------------------------*/
187194
foreach (var childTopic in topic.Children) {
188-
topics.AddRange(AddTopic(childTopic));
195+
topics.AddRange(AddTopic(childTopic, includeMetadata));
189196
}
190197

191198
return topics;

0 commit comments

Comments
 (0)