Skip to content

Commit 518d2b0

Browse files
feat: psturtle.com includes ( Fixes #180, Fixes #181 )
Adding includes for Sitemap.xml and Robots.txt and updating build.
1 parent 7372394 commit 518d2b0

File tree

3 files changed

+79
-52
lines changed

3 files changed

+79
-52
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<#
2+
.SYNOPSIS
3+
Includes robots.txt
4+
.DESCRIPTION
5+
Includes the content for `robots.txt`.
6+
7+
This can be called after a site build to generate a `robots.txt` file.
8+
.EXAMPLE
9+
./Robots.txt.ps1
10+
#>
11+
param()
12+
#region robots.txt
13+
if (-not $Site.NoRobots) {
14+
@(
15+
"User-agent: *"
16+
if ($site.Disallow) {
17+
foreach ($disallow in $site.Disallow) {
18+
"Disallow: $disallow"
19+
}
20+
}
21+
if ($site.Allow) {
22+
foreach ($allow in $site.Allow) {
23+
"Allow: $allow"
24+
}
25+
}
26+
if ($site.CNAME -and -not $site.NoSitemap) {
27+
"Sitemap: https://$($site.CNAME)/sitemap.xml"
28+
}
29+
)
30+
}
31+
#endregion robots.txt
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<#
2+
.SYNOPSIS
3+
Includes sitemap.xml
4+
.DESCRIPTION
5+
Includes the content for `sitemap.xml`.
6+
7+
This can be called after a site build to generate a `sitemap.xml` file.
8+
.EXAMPLE
9+
./Sitemap.xml.ps1
10+
#>
11+
#region sitemap.xml
12+
if (-not $Site.NoSitemap) {
13+
$siteMapXml = @(
14+
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'
15+
:nextPage foreach ($key in $site.PagesByUrl.Keys | Sort-Object { "$_".Length}) {
16+
$keyUri = $key -as [Uri]
17+
$page = $site.PagesByUrl[$key]
18+
if ($site.Disallow) {
19+
foreach ($disallow in $site.Disallow) {
20+
if ($keyUri.LocalPath -like "*$disallow*") { continue nextPage }
21+
if ($keyUri.AbsoluteUri -like "*$disallow*") { continue nextPage }
22+
}
23+
}
24+
if ($page.NoIndex) { continue }
25+
if ($page.NoSitemap) { continue }
26+
if ($page.OutputFile.Extension -ne '.html') { continue }
27+
"<url>"
28+
"<loc>$key</loc>"
29+
if ($site.PagesByUrl[$key].Date -is [DateTime]) {
30+
"<lastmod>$($site.PagesByUrl[$key].Date.ToString('yyyy-MM-dd'))</lastmod>"
31+
}
32+
"</url>"
33+
}
34+
'</urlset>'
35+
) -join ' ' -as [xml]
36+
if ($siteMapXml) {
37+
$strWriter = [IO.StringWriter]::new()
38+
$siteMapXml.Save($strWriter)
39+
"$strWriter"
40+
}
41+
}
42+
#endregion sitemap.xml

psturtle.com/build.ps1

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -149,39 +149,6 @@ if ($lastBuild) {
149149
$newLastBuild | ConvertTo-Json -Depth 2 > lastBuild.json
150150
#endregion lastBuild.json
151151

152-
#region sitemap.xml
153-
if (-not $Site.NoSitemap) {
154-
$siteMapXml = @(
155-
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'
156-
:nextPage foreach ($key in $site.PagesByUrl.Keys | Sort-Object { "$_".Length}) {
157-
$keyUri = $key -as [Uri]
158-
$page = $site.PagesByUrl[$key]
159-
if ($site.Disallow) {
160-
foreach ($disallow in $site.Disallow) {
161-
if ($keyUri.LocalPath -like "*$disallow*") { continue nextPage }
162-
if ($keyUri.AbsoluteUri -like "*$disallow*") { continue nextPage }
163-
}
164-
}
165-
if ($page.NoIndex) { continue }
166-
if ($page.NoSitemap) { continue }
167-
if ($page.OutputFile.Extension -ne '.html') { continue }
168-
"<url>"
169-
"<loc>$key</loc>"
170-
if ($site.PagesByUrl[$key].Date -is [DateTime]) {
171-
"<lastmod>$($site.PagesByUrl[$key].Date.ToString('yyyy-MM-dd'))</lastmod>"
172-
}
173-
"</url>"
174-
}
175-
'</urlset>'
176-
) -join ' ' -as [xml]
177-
if ($siteMapXml) {
178-
$siteMapXml.Save((
179-
Join-Path $site.PSScriptRoot sitemap.xml
180-
))
181-
}
182-
}
183-
#endregion sitemap.xml
184-
185152
#region index.rss
186153
if (-not $Site.NoRss) {
187154
$pagesByDate = @($site.PagesByUrl.GetEnumerator() |
@@ -251,26 +218,13 @@ if (-not $Site.NoRss) {
251218

252219
#endregion index.rss
253220

254-
#region robots.txt
255-
if (-not $Site.NoRobots) {
256-
@(
257-
"User-agent: *"
258-
if ($site.Disallow) {
259-
foreach ($disallow in $site.Disallow) {
260-
"Disallow: $disallow"
261-
}
262-
}
263-
if ($site.Allow) {
264-
foreach ($allow in $site.Allow) {
265-
"Allow: $allow"
266-
}
267-
}
268-
if ($site.CNAME -and -not $site.NoSitemap) {
269-
"Sitemap: https://$($site.CNAME)/sitemap.xml"
270-
}
271-
) > robots.txt
221+
if ($site.includes.'Sitemap.xml' -is [Management.Automation.ExternalScriptInfo]) {
222+
. $site.includes.'Sitemap.xml' > sitemap.xml
223+
}
224+
225+
if ($site.includes.'Robots.txt' -is [Management.Automation.ExternalScriptInfo]) {
226+
. $site.includes.'Robots.txt' > robots.txt
272227
}
273-
#endregion robots.txt
274228

275229
#region index.json
276230
if (-not $Site.NoIndex) {

0 commit comments

Comments
 (0)