@mk-techi/sitemap 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Moukhammad Arsamerzouev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,85 @@
1
- # Temporary Holding Version
1
+ # @mk-techi/sitemap
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Parse `sitemap.xml` files and sitemap indexes into URLs, `lastmod` dates and nested
4
+ sitemap locations, following the [sitemaps.org protocol](https://www.sitemaps.org/protocol.html).
5
+
6
+ A pure function over a string: no network, no I/O. Fetch the XML however you like (and
7
+ decompress `.xml.gz` yourself), then pass the text in.
8
+
9
+ ## Install
10
+
11
+ ```sh
12
+ npm install @mk-techi/sitemap
13
+ ```
14
+
15
+ Requires Node.js 20.18.1 or later (cheerio's minimum). ESM only. Single runtime
16
+ dependency: [cheerio](https://cheerio.js.org/), used in XML mode.
17
+
18
+ ## Example
19
+
20
+ ```ts
21
+ import { parseSitemapXml } from "@mk-techi/sitemap";
22
+
23
+ parseSitemapXml(`<?xml version="1.0" encoding="UTF-8"?>
24
+ <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
25
+ <url><loc>https://example.com/</loc><lastmod>2026-11-30</lastmod></url>
26
+ <url><loc>https://example.com/about</loc></url>
27
+ </urlset>`);
28
+ // {
29
+ // isIndex: false,
30
+ // nestedSitemaps: [],
31
+ // urls: [
32
+ // { loc: "https://example.com/", lastmod: "2026-11-30" },
33
+ // { loc: "https://example.com/about", lastmod: null }
34
+ // ]
35
+ // }
36
+
37
+ parseSitemapXml(`<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
38
+ <sitemap><loc>https://example.com/sitemap-posts.xml</loc></sitemap>
39
+ </sitemapindex>`);
40
+ // { isIndex: true, nestedSitemaps: ["https://example.com/sitemap-posts.xml"], urls: [] }
41
+ ```
42
+
43
+ ## API
44
+
45
+ ### `parseSitemapXml(xml: string): ParsedSitemap`
46
+
47
+ - `nestedSitemaps`: every `<loc>` of a `<sitemap>` inside a `<sitemapindex>`.
48
+ - `urls`: every `<url>` inside a `<urlset>`, with its `<loc>` and its `<lastmod>` (or
49
+ `null`). `lastmod` is returned as written, not parsed into a date.
50
+ - `isIndex`: `true` when the document's root element is `<sitemapindex>`, including an
51
+ empty index. A `<sitemapindex>` nested under another root does not count.
52
+ - Text is trimmed, XML entities and CDATA are decoded, and entries whose `<loc>` is
53
+ missing or blank are dropped.
54
+ - Extension elements such as `<image:loc>` are ignored.
55
+ - Never throws. Input with no sitemap elements returns empty collections.
56
+
57
+ ### Malformed XML may return partial results
58
+
59
+ The parser is lenient and **does not validate** the XML. A document that is not
60
+ well-formed is not rejected: you get whatever entries could still be read, which may
61
+ be only some of them. That covers a truncated download, mismatched or unclosed tags, an
62
+ unescaped `&`, several root elements, unquoted attributes, or stray text before the root.
63
+
64
+ ```ts
65
+ parseSitemapXml("<urlset><url><loc>https://example.com/a</loc></url><url><loc>https://exa");
66
+ // { isIndex: false, nestedSitemaps: [], urls: [{ loc: "https://example.com/a", lastmod: null }, { loc: "https://exa", lastmod: null }] }
67
+ ```
68
+
69
+ This is deliberate: a crawler usually wants the URLs a damaged sitemap still holds. If
70
+ you need to reject malformed sitemaps, validate the XML with a strict parser before
71
+ calling `parseSitemapXml`.
72
+
73
+ Elements must use the default sitemaps namespace (the usual form). Namespace-prefixed
74
+ tags such as `<sm:urlset>` are not recognised.
75
+
76
+ ### Types
77
+
78
+ ```ts
79
+ interface SitemapEntry { loc: string; lastmod: string | null }
80
+ interface ParsedSitemap { isIndex: boolean; nestedSitemaps: string[]; urls: SitemapEntry[] }
81
+ ```
82
+
83
+ ## License
84
+
85
+ MIT
@@ -0,0 +1,18 @@
1
+ /** One `<url>` entry of a urlset. `lastmod` is the raw trimmed text, or `null` when absent or blank. */
2
+ export interface SitemapEntry {
3
+ loc: string;
4
+ lastmod: string | null;
5
+ }
6
+ /** Result of parsing a sitemap or sitemap index. */
7
+ export interface ParsedSitemap {
8
+ isIndex: boolean;
9
+ nestedSitemaps: string[];
10
+ urls: SitemapEntry[];
11
+ }
12
+ /**
13
+ * Parse sitemap XML (a `<urlset>` or a `<sitemapindex>`) without fetching anything.
14
+ * Text is trimmed and entries whose `<loc>` is blank are dropped. Never throws.
15
+ * Parsing is lenient and does not validate the XML: malformed input may return
16
+ * partial results, built from whatever entries could still be read.
17
+ */
18
+ export declare function parseSitemapXml(xml: string): ParsedSitemap;
package/dist/index.js ADDED
@@ -0,0 +1,28 @@
1
+ import * as cheerio from "cheerio";
2
+ /**
3
+ * Parse sitemap XML (a `<urlset>` or a `<sitemapindex>`) without fetching anything.
4
+ * Text is trimmed and entries whose `<loc>` is blank are dropped. Never throws.
5
+ * Parsing is lenient and does not validate the XML: malformed input may return
6
+ * partial results, built from whatever entries could still be read.
7
+ */
8
+ export function parseSitemapXml(xml) {
9
+ try {
10
+ const body = xml;
11
+ const $ = cheerio.load(body, { xmlMode: true });
12
+ const nested = $("sitemapindex sitemap loc").map((_, el) => $(el).text().trim()).get();
13
+ const urls = $("urlset url")
14
+ .map((_, el) => ({
15
+ loc: $(el).find("loc").text().trim(),
16
+ lastmod: $(el).find("lastmod").text().trim() || null,
17
+ }))
18
+ .get();
19
+ return {
20
+ isIndex: $.root().children("sitemapindex").length > 0,
21
+ nestedSitemaps: nested.filter((loc) => loc !== ""),
22
+ urls: urls.filter((entry) => entry.loc !== ""),
23
+ };
24
+ }
25
+ catch {
26
+ return { isIndex: false, nestedSitemaps: [], urls: [] };
27
+ }
28
+ }
package/package.json CHANGED
@@ -1,6 +1,51 @@
1
1
  {
2
2
  "name": "@mk-techi/sitemap",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "repository": {
4
+ "type": "git",
5
+ "url": "git+https://github.com/mk-techi/sitemap.git"
6
+ },
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "version": "0.1.0",
11
+ "description": "Parse sitemap.xml and sitemap index files into URLs, lastmod dates and nested sitemaps.",
12
+ "license": "MIT",
13
+ "type": "module",
14
+ "main": "./dist/index.js",
15
+ "types": "./dist/index.d.ts",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "default": "./dist/index.js"
20
+ }
21
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "sideEffects": false,
26
+ "engines": {
27
+ "node": ">=20.18.1"
28
+ },
29
+ "keywords": [
30
+ "sitemap",
31
+ "sitemap.xml",
32
+ "sitemapindex",
33
+ "xml",
34
+ "parser",
35
+ "seo",
36
+ "crawler"
37
+ ],
38
+ "scripts": {
39
+ "build": "tsc",
40
+ "test": "npm run build && node --import tsx --test test/index.test.ts",
41
+ "prepublishOnly": "npm test"
42
+ },
43
+ "dependencies": {
44
+ "cheerio": "^1.2.0"
45
+ },
46
+ "devDependencies": {
47
+ "@types/node": "^22.20.7",
48
+ "tsx": "^4.23.15",
49
+ "typescript": "^5.9.3"
50
+ }
51
+ }