Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jdrossl committed Jul 29, 2021
0 parents commit 13d2214
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 0 deletions.
Binary file added .crafter/screenshots/cover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 63 additions & 0 deletions craftercms-plugin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# MIT License
#
# Copyright (c) 2018-2021 Crafter Software Corporation. All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# The version of the format for this file
descriptorVersion: 2

# Describe the plugin
plugin:
type: site
id: org.craftercms.plugin.sitemap
name: Site Map
tags:
- sitemap
version:
major: 1
minor: 0
patch: 0
description: Plugin to generate a site map
website:
name: Sitemap Plugin
url: https://github.com/craftercms/sitemap-plugin
media:
screenshots:
- title: Sitemap
description: Sitemap Example
url: https://raw.githubusercontent.com/craftercms/analytics-plugin/master/.crafter/cover.png
developer:
company:
name: Crafter Software
email: info@craftersoftware.com
url: https://craftersoftware.com
build:
id: c3d2a5444e6a24b5e0481d6ba87901d0b02716c8
date: 2021-07-29T00:00:00Z
license:
name: MIT
url: https://opensource.org/licenses/MIT
crafterCmsVersions:
- major: 4
minor: 0
patch: 0
crafterCmsEditions:
- community
- enterprise
80 changes: 80 additions & 0 deletions delivery/scripts/classes/SitemapItemConverter.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* MIT License
*
* Copyright (c) 2018-2021 Crafter Software Corporation. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package plugins.org.craftercms.plugin.sitemap

import org.craftercms.commons.converters.Converter
import org.craftercms.engine.model.SiteItem
import org.craftercms.engine.navigation.NavItem
import org.craftercms.engine.service.UrlTransformationService

/**
* Implementation of {@link Converter} that wraps another instance to add
* the properties related to sitemap as additional attributes.
*
* @author joseross
*/
class SitemapItemConverter implements Converter<SiteItem, NavItem> {

/**
* The actual converter to use
*/
protected Converter<SiteItem, NavItem> converter

/**
* The service to generate URLs
*/
protected UrlTransformationService urlTransformationService

SitemapItemConverter(Converter<SiteItem, NavItem> converter, UrlTransformationService urlTransformationService) {
this.converter = converter
this.urlTransformationService = urlTransformationService
}

@Override
Class<?> getSourceClass() {
return SiteItem
}

@Override
Class<?> getTargetClass() {
return NavItem
}

@Override
NavItem convert(SiteItem siteItem) {
// Generate the regular NavItem using the converter
def item = converter.convert(siteItem)

// If the item is present add the sitmap attributes
if (item) {
item.attributes.loc = urlTransformationService.transform("storeUrlToFullRenderUrl", siteItem.storeUrl)
item.attributes.lastmod = siteItem.lastModifiedDate as String
item.attributes.changefreq = siteItem.changeFrequency_s as String
item.attributes.priority = siteItem.priority_f as String
}
return item
}

}
40 changes: 40 additions & 0 deletions delivery/scripts/controllers/sitemap.get.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* MIT License
*
* Copyright (c) 2018-2021 Crafter Software Corporation. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

import plugins.org.craftercms.plugin.sitemap.SitemapItemConverter

// Make sure the response uses the right content-type & encoding
response.contentType = "application/xml"
response.characterEncoding = "UTF-8"

// Create the custom converter required
def converter = new SitemapItemConverter(navTreeBuilder.defaultItemConverter, urlTransformationService)

// Make the data available for the template
templateModel.pages = navTreeBuilder.getNavTree("/site/website", 2, "", converter)
.subItems
.collect { it -> it.attributes }

// Return the template to render
return "/templates/plugins/org/craftercms/plugin/sitemap/sitemap.ftl"
40 changes: 40 additions & 0 deletions delivery/templates/sitemap.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!--
~ MIT License
~
~ Copyright (c) 2018-2021 Crafter Software Corporation. All Rights Reserved.
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy
~ of this software and associated documentation files (the "Software"), to deal
~ in the Software without restriction, including without limitation the rights
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
~ copies of the Software, and to permit persons to whom the Software is
~ furnished to do so, subject to the following conditions:
~
~ The above copyright notice and this permission notice shall be included in all
~ copies or substantial portions of the Software.
~
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
~ SOFTWARE.
-->
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<#list pages as page>
<url>
<loc>${page.loc}</loc>
<#if page.lastmod?has_content>
<lastmod>${page.lastmod}</lastmod>
</#if>
<#if page.changefreq?has_content>
<changefreq>${page.changefreq}</changefreq>
</#if>
<#if page.priority?has_content>
<priority>${page.priority}</priority>
</#if>
</url>
</#list>
</urlset>
39 changes: 39 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Sitemap Plugin for Crafter CMS

This is a plugin to generate a sitemap for your site.

# Installation

The plugin can be installed to your site from the Crafter CMS Marketplace

# Setup

After the plugin has been installed you can test the result by opening the URL
http://localhost:8080/plugins/org/craftercms/plugin/sitemap/sitemap

The plugin will generate an XML sitemap for the top level pages that are configured with `Place in Nav`, for more
information check the [documentation](https://docs.craftercms.org/en/4.0/developers/form-controls/form-page-order.html).

It is also possible to customize every page with sitemap specific fields:

|Field Type|Field Name|Description|
|:-:|:-:|-|
|`input`|`changeFrequency_s`|Indicates how frequently the page is likely to change. Possible values: always, hourly, daily, weekly, monthly, yearly, never|
|`input`|`priority_f`|Indicates the priority of the page. Possible values: from 0.0 to 1.0|

When the sitemap looks correct a URL rewrite rule can be used to make it available under the main URL of your site:

```xml
<urlrewrite>

<rule>
<from>^/sitemap.xml$</from>
<to>/plugins/org/craftercms/plugin/sitemap/sitemap</to>
</rule>

</urlrewrite>
```

For more information about the rewrite rules check the [documentation](https://docs.craftercms.org/en/4.0/site-administrators/engine/configure-url-rewrite.html).

Now the sitemap will be available at http://localhost:8080/sitemap.xml and ready to be submitted to a search engine.

0 comments on commit 13d2214

Please sign in to comment.