Skip to content

Instantly share code, notes, and snippets.

@resting
Forked from evantahler/buildSitemap.js
Created July 11, 2018 00:20
Show Gist options
  • Select an option

  • Save resting/d884925015bc7eed34054f1bea9539c0 to your computer and use it in GitHub Desktop.

Select an option

Save resting/d884925015bc7eed34054f1bea9539c0 to your computer and use it in GitHub Desktop.

Revisions

  1. @evantahler evantahler revised this gist May 30, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion buildSitemap.js
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ diskPages.forEach((page) => {
    page = `${SITE_ROOT}${page}`

    if (page.match(/.*\/index$/)) {
    page = page.replace(/(.*\\)index$/, '$1\\')
    page = page.replace(/(.*)index$/, '$1')
    }

    xml += '<url>'
  2. @evantahler evantahler revised this gist May 29, 2017. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion buildSitemap.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@
    #! /usr/bin/env node

    // I am ./bin/buildSitemap.js

    const path = require('path')
  3. @evantahler evantahler revised this gist May 29, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions buildSitemap.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    #! /usr/bin/env node

    // I am ./bin/buildSitemap.js

    const path = require('path')
    const glob = require('glob')
    const fs = require('fs')
  4. @evantahler evantahler revised this gist May 29, 2017. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions buildSitemap.js
    Original file line number Diff line number Diff line change
    @@ -27,8 +27,6 @@ diskPages.forEach((page) => {
    page = page.replace(/(.*\\)index$/, '$1\\')
    }

    console.log(page)

    xml += '<url>'
    xml += `<loc>${page}</loc>`
    xml += `<lastmod>${lastMod}</lastmod>`
  5. @evantahler evantahler revised this gist May 29, 2017. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion buildSitemap.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    #! /usr/bin/env node
    // I am ./bin/buildSitemap.js

    const path = require('path')
    const glob = require('glob')
    const fs = require('fs')
    @@ -24,6 +23,12 @@ diskPages.forEach((page) => {
    page = page.replace(/.js$/, '')
    page = `${SITE_ROOT}${page}`

    if (page.match(/.*\/index$/)) {
    page = page.replace(/(.*\\)index$/, '$1\\')
    }

    console.log(page)

    xml += '<url>'
    xml += `<loc>${page}</loc>`
    xml += `<lastmod>${lastMod}</lastmod>`
  6. @evantahler evantahler created this gist May 29, 2017.
    39 changes: 39 additions & 0 deletions buildSitemap.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    #! /usr/bin/env node
    // I am ./bin/buildSitemap.js

    const path = require('path')
    const glob = require('glob')
    const fs = require('fs')

    const SITE_ROOT = process.env.SITE_ROOT || 'https://www.actionherojs.com'
    const SOURCE = process.env.SOURCE || path.join(__dirname, '..', 'pages', '/**/*.js')
    const DESTINATION = process.env.DESTINATION || path.join(__dirname, '..', 'static', 'sitemap.xml')

    let diskPages = glob.sync(SOURCE)

    let xml = ''
    xml += '<?xml version="1.0" encoding="UTF-8"?>'
    xml += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'

    diskPages.forEach((page) => {
    let stats = fs.statSync(page)
    let modDate = new Date(stats.mtime)
    let lastMod = `${modDate.getFullYear()}-${('0' + (modDate.getMonth() + 1)).slice(-2)}-${('0' + modDate.getDate()).slice(-2)}`

    page = page.replace(path.join(__dirname, '..', 'pages'), '')
    page = page.replace(/.js$/, '')
    page = `${SITE_ROOT}${page}`

    xml += '<url>'
    xml += `<loc>${page}</loc>`
    xml += `<lastmod>${lastMod}</lastmod>`
    xml += `<changefreq>always</changefreq>`
    xml += `<priority>0.5</priority>`
    xml += '</url>'
    })

    xml += '</urlset>'

    fs.writeFileSync(DESTINATION, xml)

    console.log(`Wrote sitemap for ${diskPages.length} pages to ${DESTINATION}`)