Package: datasette-sitemap
datasette-sitemapGenerate sitemap.xml for Datasette sitesInstallationInstall this plugin in the same environment as Datasette.datasette install datasette-sitemapDemoThis plugin is used for the sitemap ontil.simonwillison.net:https://til.simonwillison.net/sitemap.xmlHere'sthe configurationused for that sitemap.UsageOnce configured, this plugin adds a sitemap at/sitemap.xmlwith a list of URLs.This list is defined using a SQL query inmetadata.json(or.yml) that looks like this:{"plugins":{"datasette-sitemap":{"query":"select '/' || id as path from my_table"}}}Usingmetadata.ymlallows for multi-line SQL queries which can be easier to maintain:plugins:datasette-sitemap:query:|select'/' || id as pathfrommy_tableThe SQL query must return a column calledpath. The values in this column must begin with a/. They will be used to generate a sitemap that looks like this:<?xml version="1.0" encoding="UTF-8"?><urlsetxmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://example.com/1</loc></url><url><loc>https://example.com/2</loc></url></urlset>You can useUNIONin your SQL query to combine results from multiple tables, or include literal paths that you want to include in the index:select'/data/table1/'||idaspathfromtable1unionselect'/data/table2/'||idaspathfromtable2unionselect'/about'aspathIf your Datasette instance has multiple databases you can configure the database to query using thedatabaseconfiguration property.By default the domain name for the genearted URLs in the sitemap will be detected from the incoming request.You can setbase_urlinstead to override this. This should not include a trailing slash.This example shows both of those settings, running the query against thecontentdatabase and setting a custom base URL:plugins:datasette-sitemap:query:|select '/plugins/' || name as path from pluginsunionselect '/tools/' || name as path from toolsunionselect '/news' as pathdatabase:contentbase_url:https://datasette.ioTry that query.robots.txtThis plugin adds arobots.txtfile pointing to the sitemap:Sitemap: http://example.com/sitemap.xmlYou can take full control of the sitemap by installing and configuring thedatasette-block-robotsplugin.This plugin will add theSitemap:line even if you are usingdatasette-block-robotsfor the rest of yourrobots.txtfile.Adding paths to the sitemap from other pluginsThis plugin adds a newplugin hookto Datasete calledsitemap_extra_paths()which can be used by other plugins to add their own additional lines to thesitemap.xmlfile.The hook accepts these optional parameters:datasette: The currentDatasette instance. You can use this to execute SQL queries or read plugin configuration settings.request: TheRequest objectrepresenting the incoming request to/sitemap.xml.The hook should return a list of strings, each representing a path to be added to the sitemap. Each path must begin with a/.It can also return anasync deffunction, which will be awaited and used to generate a list of lines. Use this option if you need to makeawaitcalls inside you hook implementation.This example uses the hook to add two extra paths, one of which came from a SQL query:fromdatasetteimporthookimpl@hookimpldefsitemap_extra_paths(datasette):asyncdefinner():db=datasette.get_database()path_from_db=(awaitdb.execute("select '/example'")).single_value()return["/about",path_from_db]returninnerDevelopmentTo set up this plugin locally, first checkout the code. Then create a new virtual environment:cd datasette-sitemap
python3 -m venv venv
source venv/bin/activateNow install the dependencies and test dependencies:pip install -e '.[test]'To run the tests:pytest