obby-parser 1.0.0 → 1.0.1

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 CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 Caden McArthur
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.
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Caden McArthur
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,41 +1,41 @@
1
- # Obby-Parser
2
-
3
- A simple TypeScript library for parsing Obsidian-style markdown. It converts `[[wikilinks]]` to HTML anchor tags and collects all unique links.
4
-
5
- ## Features
6
-
7
- - Converts `[[Page Name]]` to `<a href="route/Page-Name">Page Name</a>`
8
- - Collects all wikilinks in a Set
9
- - Supports standard markdown formatting via [markdown-it](https://github.com/markdown-it/markdown-it)
10
-
11
- ## Installation
12
-
13
- ```bash
14
- npm install obsi-parser
15
- ```
16
-
17
- ## Usage
18
-
19
- ```typescript
20
- import { parseMarkdown } from 'obsi-parser';
21
-
22
- const markdown = "This references [[My Page]] and also **bold** text.";
23
- const result = parseMarkdown(markdown, "pages/");
24
-
25
- console.log(result.html); // HTML output with anchor tags
26
- console.log(result.links_set); // Set { "My Page" }
27
- ```
28
-
29
- ## API
30
-
31
- ### `parseMarkdown(markdown: string, route: string): ParseResult`
32
-
33
- - `markdown`: The markdown string to parse.
34
- - `route`: The base path for generated links.
35
- - Returns:
36
- - `html`: The resulting HTML string.
37
- - `links_set`: A Set of all unique wikilinks found.
38
-
39
- ## License
40
-
1
+ # Obby-Parser
2
+
3
+ A simple TypeScript library for parsing Obsidian-style markdown. It converts `[[wikilinks]]` to HTML anchor tags and collects all unique links.
4
+
5
+ ## Features
6
+
7
+ - Converts `[[Page Name]]` to `<a href="route/Page-Name">Page Name</a>`
8
+ - Collects all wikilinks in a Set
9
+ - Supports standard markdown formatting via [markdown-it](https://github.com/markdown-it/markdown-it)
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ npm install obsi-parser
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```typescript
20
+ import { parseMarkdown } from 'obsi-parser';
21
+
22
+ const markdown = "This references [[My Page]] and also **bold** text.";
23
+ const result = parseMarkdown(markdown, "pages/");
24
+
25
+ console.log(result.html); // HTML output with anchor tags
26
+ console.log(result.links_set); // Set { "My Page" }
27
+ ```
28
+
29
+ ## API
30
+
31
+ ### `parseMarkdown(markdown: string, route: string): ParseResult`
32
+
33
+ - `markdown`: The markdown string to parse.
34
+ - `route`: The base path for generated links.
35
+ - Returns:
36
+ - `html`: The resulting HTML string.
37
+ - `links_set`: A Set of all unique wikilinks found.
38
+
39
+ ## License
40
+
41
41
  MIT
package/dist/index.js CHANGED
@@ -19,7 +19,7 @@ const md = new markdown_it_1.default({ html: true });
19
19
  * For now, this is minimal: trim and replace spaces with dashes.
20
20
  */
21
21
  function normalizeLinkText(text) {
22
- return text.trim().replace(/\s+/g, "-");
22
+ return text.trim().replace(/\s+/g, "").toLowerCase();
23
23
  }
24
24
  /**
25
25
  * Replace [[link]] occurrences with a placeholder anchor and collect links.
@@ -31,6 +31,7 @@ function transformWikilinks(input, route, links) {
31
31
  const href = `/${normalizeLinkText(display)}`;
32
32
  links.add(display);
33
33
  // Return markdown inline HTML anchor; markdown-it will preserve it
34
+ //for ease of use, this will return a stripped down version of the url
34
35
  return `<a href="${href}" target="_blank" rel="noopener noreferrer">${display}</a>`;
35
36
  });
36
37
  }
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;;;AAsCH,sCAKC;AAzCD,8DAAqC;AAOrC,MAAM,EAAE,GAAG,IAAI,qBAAU,CAAC,EAAC,IAAI,EAAE,IAAI,EAAC,CAAC,CAAC;AAExC;;;GAGG;AACH,SAAS,iBAAiB,CAAC,IAAY;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AEG;AACH,SAAS,kBAAkB,CAAC,KAAa,EAAE,KAAa,EAAE,KAAkB;IAC1E,wDAAwD;IACxD,OAAO,KAAK,CAAC,OAAO,CAAC,mBAAmB,EAAE,CAAC,CAAC,EAAE,KAAa,EAAE,EAAE;QAC7D,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC;QAC7B,MAAM,IAAI,GAAG,IAAI,iBAAiB,CAAC,OAAO,CAAC,EAAE,CAAC;QAC9C,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QACnB,mEAAmE;QACnE,sEAAsE;QACtE,OAAO,YAAY,IAAI,+CAA+C,OAAO,MAAM,CAAC;IACtF,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;GAGG;AACH,SAAgB,aAAa,CAAC,QAAgB,EAAE,KAAa;IAC3D,MAAM,SAAS,GAAgB,IAAI,GAAG,EAAE,CAAC;IACzC,MAAM,WAAW,GAAG,kBAAkB,CAAC,QAAQ,EAAE,KAAK,EAAE,SAAS,CAAC,CAAC;IACnE,MAAM,IAAI,GAAG,EAAE,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IACpC,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC;AAC7B,CAAC"}
package/package.json CHANGED
@@ -1,38 +1,38 @@
1
- {
2
- "name": "obby-parser",
3
- "version": "1.0.0",
4
- "description": "Parse Obsidian-style markdown files including ([[links]]) into HTML and extract links",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
7
- "scripts": {
8
- "clean": "rimraf dist",
9
- "build": "tsc -p tsconfig.build.json",
10
- "dev": "ts-node src/example.ts",
11
- "test": "vitest",
12
- "lint": "eslint \"src/**/*.ts\"",
13
- "prepare": "npm run build"
14
- },
15
- "keywords": [
16
- "obsidian",
17
- "markdown",
18
- "parser",
19
- "npx",
20
- "typescript"
21
- ],
22
- "author": "cmac2112",
23
- "license": "MIT",
24
- "dependencies": {
25
- "markdown-it": "^13.0.2"
26
- },
27
- "devDependencies": {
28
- "@types/markdown-it": "^14.1.2",
29
- "@types/node": "^20.0.0",
30
- "@typescript-eslint/eslint-plugin": "^5.0.0",
31
- "@typescript-eslint/parser": "^5.0.0",
32
- "eslint": "^8.0.0",
33
- "rimraf": "^5.0.0",
34
- "ts-node": "^10.9.1",
35
- "typescript": "^5.0.0",
36
- "vitest": "^1.4.0"
37
- }
38
- }
1
+ {
2
+ "name": "obby-parser",
3
+ "version": "1.0.1",
4
+ "description": "Parse Obsidian-style markdown files including ([[links]]) into HTML and extract links",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "clean": "rimraf dist",
9
+ "build": "tsc -p tsconfig.build.json",
10
+ "dev": "ts-node src/example.ts",
11
+ "test": "vitest",
12
+ "lint": "eslint \"src/**/*.ts\"",
13
+ "prepare": "npm run build"
14
+ },
15
+ "keywords": [
16
+ "obsidian",
17
+ "markdown",
18
+ "parser",
19
+ "npx",
20
+ "typescript"
21
+ ],
22
+ "author": "cmac2112",
23
+ "license": "MIT",
24
+ "dependencies": {
25
+ "markdown-it": "^13.0.2"
26
+ },
27
+ "devDependencies": {
28
+ "@types/markdown-it": "^14.1.2",
29
+ "@types/node": "^20.0.0",
30
+ "@typescript-eslint/eslint-plugin": "^5.0.0",
31
+ "@typescript-eslint/parser": "^5.0.0",
32
+ "eslint": "^8.0.0",
33
+ "rimraf": "^5.0.0",
34
+ "ts-node": "^10.9.1",
35
+ "typescript": "^5.0.0",
36
+ "vitest": "^1.4.0"
37
+ }
38
+ }
package/src/example.ts CHANGED
@@ -1,15 +1,15 @@
1
- import fs from "fs";
2
- import path from "path";
3
- import { parseMarkdown } from "./index";
4
-
5
- const input = process.argv[2] || path.join(process.cwd(), "example.md");
6
- if (!fs.existsSync(input)) {
7
- console.error("No input file found:", input);
8
- process.exit(1);
9
- }
10
- const content = fs.readFileSync(input, "utf8");
11
- const result = parseMarkdown(content, "www.personalwebsite.com/test/");
12
- console.log("HTML output:\n", result.html);
13
- console.log("\nLinks found:", result.links_set);
14
-
15
- console.log("json parsed", JSON.stringify(result.html))
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import { parseMarkdown } from "./index";
4
+
5
+ const input = process.argv[2] || path.join(process.cwd(), "example.md");
6
+ if (!fs.existsSync(input)) {
7
+ console.error("No input file found:", input);
8
+ process.exit(1);
9
+ }
10
+ const content = fs.readFileSync(input, "utf8");
11
+ const result = parseMarkdown(content, "www.personalwebsite.com/test/");
12
+ console.log("HTML output:\n", result.html);
13
+ console.log("\nLinks found:", result.links_set);
14
+
15
+ console.log("json parsed", JSON.stringify(result.html))
package/src/index.ts CHANGED
@@ -1,51 +1,52 @@
1
- /**
2
- * parseMarkdown
3
- * - Accepts an Obsidian-style markdown string.
4
- * - Finds all [[wikilinks]] and collects them into links[].
5
- * - Replaces [[Name]] with an <a href="Name.html">Name</a> (simple default transform).
6
- * - Returns the HTML (via markdown-it) and the array of links found.
7
- * - This is a one way operation meant to just translate markdown files for the web
8
- */
9
-
10
- import MarkdownIt from "markdown-it";
11
-
12
- export type ParseResult = {
13
- html: string;
14
- links_set: Set<string>;
15
- };
16
-
17
- const md = new MarkdownIt({html: true});
18
-
19
- /**
20
- * Normalize link text to a safe file name or slug.
21
- * For now, this is minimal: trim and replace spaces with dashes.
22
- */
23
- function normalizeLinkText(text: string): string {
24
- return text.trim().replace(/\s+/g, "-");
25
-
26
- }
27
-
28
- /**
29
- * Replace [[link]] occurrences with a placeholder anchor and collect links.
30
- */
31
- function transformWikilinks(input: string, route: string, links: Set<string>): string {
32
- // Use a replacer so we can capture multiple occurrences
33
- return input.replace(/\[\[([^\]]+)\]\]/g, (_, inner: string) => {
34
- const display = inner.trim();
35
- const href = `/${normalizeLinkText(display)}`;
36
- links.add(display);
37
- // Return markdown inline HTML anchor; markdown-it will preserve it
38
- return `<a href="${href}" target="_blank" rel="noopener noreferrer">${display}</a>`;
39
- });
40
- }
41
-
42
- /**
43
- * Main exported function.
44
- * route should be setup in a way for you to handle params easily
45
- */
46
- export function parseMarkdown(markdown: string, route: string): ParseResult {
47
- const links_set: Set<string> = new Set();
48
- const withAnchors = transformWikilinks(markdown, route, links_set);
49
- const html = md.render(withAnchors);
50
- return { html, links_set };
1
+ /**
2
+ * parseMarkdown
3
+ * - Accepts an Obsidian-style markdown string.
4
+ * - Finds all [[wikilinks]] and collects them into links[].
5
+ * - Replaces [[Name]] with an <a href="Name.html">Name</a> (simple default transform).
6
+ * - Returns the HTML (via markdown-it) and the array of links found.
7
+ * - This is a one way operation meant to just translate markdown files for the web
8
+ */
9
+
10
+ import MarkdownIt from "markdown-it";
11
+
12
+ export type ParseResult = {
13
+ html: string;
14
+ links_set: Set<string>;
15
+ };
16
+
17
+ const md = new MarkdownIt({html: true});
18
+
19
+ /**
20
+ * Normalize link text to a safe file name or slug.
21
+ * For now, this is minimal: trim and replace spaces with dashes.
22
+ */
23
+ function normalizeLinkText(text: string): string {
24
+ return text.trim().replace(/\s+/g, "").toLowerCase();
25
+
26
+ }
27
+
28
+ /**
29
+ * Replace [[link]] occurrences with a placeholder anchor and collect links.
30
+ */
31
+ function transformWikilinks(input: string, route: string, links: Set<string>): string {
32
+ // Use a replacer so we can capture multiple occurrences
33
+ return input.replace(/\[\[([^\]]+)\]\]/g, (_, inner: string) => {
34
+ const display = inner.trim();
35
+ const href = `/${normalizeLinkText(display)}`;
36
+ links.add(display);
37
+ // Return markdown inline HTML anchor; markdown-it will preserve it
38
+ //for ease of use, this will return a stripped down version of the url
39
+ return `<a href="${href}" target="_blank" rel="noopener noreferrer">${display}</a>`;
40
+ });
41
+ }
42
+
43
+ /**
44
+ * Main exported function.
45
+ * route should be setup in a way for you to handle params easily
46
+ */
47
+ export function parseMarkdown(markdown: string, route: string): ParseResult {
48
+ const links_set: Set<string> = new Set();
49
+ const withAnchors = transformWikilinks(markdown, route, links_set);
50
+ const html = md.render(withAnchors);
51
+ return { html, links_set };
51
52
  }
@@ -1,25 +1,25 @@
1
- import { describe, it, expect } from "vitest";
2
- import { parseMarkdown } from "../src/index";
3
-
4
- let pageTest: Set<string> = new Set<string>();
5
- let linkTest: Set<string> = new Set<string>();
6
- describe("parseMarkdown", () => {
7
- it("converts [[wikilink]] to an anchor and collects link", () => {
8
- const input = "This references [[My Page]] and also inline **bold** text.";
9
- const { html, links_set } = parseMarkdown(input, "test/");
10
- pageTest.add("My Page")
11
-
12
- expect(links_set).toEqual(pageTest);
13
- expect(html).toContain('&lt;a href=&quot; test/My-Page&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;My Page&lt;/a&gt;');
14
- expect(html).toContain("<strong>bold</strong>");
15
- });
16
-
17
- it("handles multiple wikilinks", () => {
18
- const input = "[[One]] and [[Two]] and again [[One]]";
19
- const { links_set } = parseMarkdown(input, "test/");
20
- linkTest.add("One")
21
- linkTest.add("Two")
22
- expect(links_set).toEqual(linkTest);
23
- });
24
- });
1
+ import { describe, it, expect } from "vitest";
2
+ import { parseMarkdown } from "../src/index";
3
+
4
+ let pageTest: Set<string> = new Set<string>();
5
+ let linkTest: Set<string> = new Set<string>();
6
+ describe("parseMarkdown", () => {
7
+ it("converts [[wikilink]] to an anchor and collects link", () => {
8
+ const input = "This references [[My Page]] and also inline **bold** text.";
9
+ const { html, links_set } = parseMarkdown(input, "test/");
10
+ pageTest.add("My Page")
11
+
12
+ expect(links_set).toEqual(pageTest);
13
+ expect(html).toContain('<p>This references <a href="/mypage" target="_blank" rel="noopener noreferrer">My Page</a> and also inline <strong>bold</strong> text.</p>');
14
+ expect(html).toContain("<strong>bold</strong>");
15
+ });
16
+
17
+ it("handles multiple wikilinks", () => {
18
+ const input = "[[One]] and [[Two]] and again [[One]]";
19
+ const { links_set } = parseMarkdown(input, "test/");
20
+ linkTest.add("One")
21
+ linkTest.add("Two")
22
+ expect(links_set).toEqual(linkTest);
23
+ });
24
+ });
25
25
  // more robust tests are needed
@@ -1,9 +1,9 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "declaration": true,
5
- "outDir": "dist",
6
- "composite": false
7
- },
8
- "include": ["src/**/*"]
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "declaration": true,
5
+ "outDir": "dist",
6
+ "composite": false
7
+ },
8
+ "include": ["src/**/*"]
9
9
  }
package/tsconfig.json CHANGED
@@ -1,18 +1,18 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2020",
4
- "module": "CommonJS",
5
- "outDir": "./dist",
6
- "rootDir": "./src",
7
- "moduleResolution": "Node",
8
- "esModuleInterop": true,
9
- "skipLibCheck": true,
10
- "strict": true,
11
- "forceConsistentCasingInFileNames": true,
12
- "sourceMap": true,
13
- "baseUrl": ".",
14
- "resolveJsonModule": true
15
- },
16
- "include": ["src/index.ts"],
17
- "exclude": ["node_modules", "dist","test"]
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "CommonJS",
5
+ "outDir": "./dist",
6
+ "rootDir": "./src",
7
+ "moduleResolution": "Node",
8
+ "esModuleInterop": true,
9
+ "skipLibCheck": true,
10
+ "strict": true,
11
+ "forceConsistentCasingInFileNames": true,
12
+ "sourceMap": true,
13
+ "baseUrl": ".",
14
+ "resolveJsonModule": true
15
+ },
16
+ "include": ["src/index.ts"],
17
+ "exclude": ["node_modules", "dist","test"]
18
18
  }