-
Notifications
You must be signed in to change notification settings - Fork 2
/
sitemap.plugin.coffee
132 lines (103 loc) · 3.76 KB
/
sitemap.plugin.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Export Plugin
module.exports = (BasePlugin) ->
# Requires
# Node modules
path = require('path')
fs = require('fs')
url = require('url')
util = require('util')
# DocPad
balUtil = require('bal-util')
# External
_ = require('underscore')
sm = require('sitemap')
# Define Plugin
class SitemapPlugin extends BasePlugin
# Plugin name
name: 'sitemap'
# --------------
# Configuration values
# default values
config:
sitemapPath: "" # populated in the constructor
# Default values
# the following defaults are for the whole sitemap.xml file
defaultsGlobal:
hostname: "http://example.com" # Obviously need to be changed
cachetime: 600000 # 600 seconds cache period
# the following defaults are per-document
defaultsPerDoc:
changefreq: 'weekly'
priority: 0.5
# The sitemap being built, to be passed to sitemap.js
sitemap:
hostname: null
cachetime: null
urls: []
# --------------
# Constructor
constructor: ->
super
# filesPaths[1] points to 'src/public', which we want here
@config.sitemapPath = path.normalize "#{@docpad.config.filesPaths[1]}/sitemap.xml"
# --------------
# Docpad events
# Create the sitemap.xml site-wide data at the very beginning,
# so that DocPad copies it to the `out` directory
generateBefore: ({}, next) ->
# Prepare
config = @config
sitemap = @sitemap
logger = @docpad.logger
if path.existsSync config.sitemapPath
logger.log "debug", "The sitemap.xml file already exists"
# Done, let DocPad proceed
next?()
else
logger.log "debug", "Creating the sitemap.xml file..."
balUtil.writeFile config.sitemapPath, '', (err) ->
return next?(err) if err
logger.log "debug", "Created the sitemap.xml file."
# Creates the site-wide sitemap data
_.extend sitemap, config.defaultsGlobal
# Done, let DocPad proceed
next?()
# Populate the sitemap.xml data for each document
renderDocument: (opts, next) ->
# Prepare
{extension,templateData,file} = opts
config = @config
logger = @docpad.logger
sitemap = @sitemap
# Only HTML documents are of interest for a sitemap
# todo: figure out how to reliably differenciate between document from other files (layouts, assets etc.)
if extension in ['html']
# Merge document's sitemap data and default values into document's metadata
templateData = _.extend templateData, config.defaultsPerDoc, templateData
# Create document's data
docMap =
url: file.get 'url'
changefreq: templateData.changefreq
priority: templateData.priority
logger.log "debug", "sitemap.xml data => url: #{docMap.url} - changefreq: #{docMap.changefreq} - priority: #{docMap.priority}"
# Add document data to site-wide map
sitemap.urls.push docMap
# Done, let DocPad proceed
next?()
# Write the sitemap.xml file in 'src/public' before DocPad moves it to 'out'
writeBefore: ({}, next) ->
# Prepare
config = @config
logger = @docpad.logger
sitemap = @sitemap
logger.log "debug", "Sitemap data :\n#{JSON.stringify sitemap, null, 4}"
# Create a sitemap.js object
sitemap = sm.createSitemap(sitemap);
logger.log "debug", "sitemap.xml file content :\n#{sitemap.toString()}"
# Fill the sitemap.xml file with data
balUtil.writeFile config.sitemapPath, sitemap.toString(), (err) ->
# Check
return next?(err) if err
logger.log 'debug', "Wrote the sitemap.xml file to: #{config.sitemapPath}"
# Done, let DocPad proceed
next?()