@gw2/markup-parser 1.0.0 → 1.1.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.
@@ -0,0 +1,22 @@
1
+ import { Root } from "@gw2/markup-ast";
2
+
3
+ //#region src/parser.d.ts
4
+ interface ParserOptions {
5
+ /**
6
+ * Controls whether position information is included in the output AST.
7
+ * If position information is not needed, this can be disabled to improve performance.
8
+ *
9
+ * @default true
10
+ */
11
+ includePosition?: boolean;
12
+ }
13
+ /**
14
+ * Parses a string with Guild Wars 2 markup into an abstract syntax tree (AST).
15
+ *
16
+ * @param input The input string to parse. Non-string inputs will be treated as empty strings.
17
+ * @param options Parser options.
18
+ * @returns The root node of the parsed abstract syntax tree (AST).
19
+ */
20
+ declare function parseGw2Markup(input: unknown, options?: ParserOptions): Root;
21
+ //#endregion
22
+ export { ParserOptions, parseGw2Markup };
package/dist/parser.js ADDED
@@ -0,0 +1,6 @@
1
+ function e(){let e={line:1,column:1,offset:0};return{point:()=>n(e),advance:r=>{let i=n(e);return t(e,r),{start:i,end:n(e)}}}}function t(e,t){if(e.offset!==void 0&&(e.offset+=t.length),t===`
2
+ `){e.line+=1,e.column=1;return}if(!t.includes(`
3
+ `)){e.column+=t.length;return}for(let n=0;n<t.length;n++)t.charCodeAt(n)===10?(e.line+=1,e.column=1):e.column+=1}function n(e){return{line:e.line,column:e.column,offset:e.offset}}const r=/^<c([^>]*)>/i,i=/^<\/c([^>]*)>/i,a=/^<br\s*\/?>/i;function o(t,n={}){let o=typeof t==`string`?t:``,c=n.includePosition??!0?e():void 0,l={type:`root`,children:[],position:c?{start:c.point(),end:c.point()}:void 0},u=[],d=l.children,f=0;for(;f<o.length;){let e=o[f];if(e===`
4
+ `){d.push({type:`break`,position:c?.advance(`
5
+ `)}),f+=1;continue}if(e===`<`){let e=o.slice(f),t=e.match(a);if(t){d.push({type:`break`,position:c?.advance(t[0])}),f+=t[0].length;continue}let n=e.match(r);if(n){let e=s(n[1]?.trim()??``);if(e===``||e===`/`){let e=c?.advance(n[0]),t=u.pop();t&&e&&(t.position.end=e.end),d=u.length>0?u[u.length-1].children:l.children,f+=n[0].length;continue}let t={type:`color`,color:e,children:[],position:c?.advance(n[0])};d.push(t),u.push(t),d=t.children,f+=n[0].length;continue}let p=e.match(i);if(p){let e=c?.advance(p[0]),t=u.pop();t&&e&&(t.position.end=e.end),d=u.length>0?u[u.length-1].children:l.children,f+=p[0].length;continue}}let t=o.indexOf(`<`,f+1),n=o.indexOf(`
6
+ `,f+1),p=o.length;t!==-1&&(p=t),n!==-1&&n<p&&(p=n);let m=o.slice(f,p),h=d[d.length-1],g=c?.advance(m);h?.type===`text`?(h.value+=m,g&&(h.position.end=g.end)):d.push({type:`text`,value:m,position:g}),f=p}if(c){for(let e of u)e.position.end=c.point();l.position.end=c.point()}return l}function s(e){return e.startsWith(`=#`)?`#${e.slice(2)}`:e.startsWith(`=@`)||e.startsWith(`@=`)?`@${e.slice(2)}`:e}export{o as parseGw2Markup};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gw2/markup-parser",
3
- "version": "1.0.0",
3
+ "version": "1.1.1",
4
4
  "description": "Guild Wars 2 markup parser",
5
5
  "keywords": [
6
6
  "gw2",
@@ -13,8 +13,8 @@
13
13
  "type": "module",
14
14
  "exports": {
15
15
  ".": {
16
- "types": "./dist/parser.d.mts",
17
- "default": "./dist/parser.mjs"
16
+ "types": "./dist/parser.d.ts",
17
+ "default": "./dist/parser.js"
18
18
  },
19
19
  "./package.json": "./package.json"
20
20
  },
@@ -25,7 +25,7 @@
25
25
  "license": "MIT",
26
26
  "publishConfig": {
27
27
  "access": "public",
28
- "provenance": false
28
+ "provenance": true
29
29
  },
30
30
  "repository": {
31
31
  "type": "git",
@@ -36,7 +36,7 @@
36
36
  "url": "https://github.com/gw2treasures/gw2-markup/issues"
37
37
  },
38
38
  "dependencies": {
39
- "@gw2/markup-ast": "1.0.0"
39
+ "@gw2/markup-ast": "^1.0.2"
40
40
  },
41
41
  "devDependencies": {
42
42
  "tsdown": "0.20.3",
@@ -45,7 +45,8 @@
45
45
  "vitest": "4.0.18"
46
46
  },
47
47
  "scripts": {
48
- "build": "tsdown src/parser.ts --minify",
48
+ "bench": "vitest bench",
49
+ "build": "tsdown src/parser.ts",
49
50
  "test": "vitest run",
50
51
  "test:watch": "vitest"
51
52
  }
package/dist/parser.d.mts DELETED
@@ -1,6 +0,0 @@
1
- import { Root } from "@gw2/markup-ast";
2
-
3
- //#region src/parser.d.ts
4
- declare function parseGw2Markup(input: unknown): Root;
5
- //#endregion
6
- export { parseGw2Markup };
package/dist/parser.mjs DELETED
@@ -1,4 +0,0 @@
1
- function e(){return{line:1,column:1,offset:0}}function t(e,t){for(let n of t)e.offset+=1,n===`
2
- `?(e.line+=1,e.column=1):e.column+=1}function n(e){return{...e}}const r=/^<c([^>]*)>/i,i=/^<\/c([^>]*)>/i,a=/^<br\s*\/?>/i;function o(o){let c=typeof o==`string`?o:``,l=e(),u={type:`root`,children:[],position:{start:n(l),end:n(l)}},d=[],f=()=>d.length?d[d.length-1].children:u.children,p=0;for(;p<c.length;){let e=c.slice(p);if(e[0]===`
3
- `){let e=n(l);t(l,`
4
- `),f().push({type:`break`,position:{start:e,end:n(l)}}),p+=1;continue}let o=e.match(a);if(o){let e=n(l);t(l,o[0]),f().push({type:`break`,position:{start:e,end:n(l)}}),p+=o[0].length;continue}let u=e.match(r);if(u){let e=s(u[1]?.trim()??``);if(e===``||e===`/`){t(l,u[0]);let e=d.pop();e&&(e.position.end=n(l)),p+=u[0].length;continue}let r=n(l);t(l,u[0]);let i={type:`color`,color:e,children:[],position:{start:r,end:n(l)}};f().push(i),d.push(i),p+=u[0].length;continue}let m=e.match(i);if(m){t(l,m[0]);let e=d.pop();e&&(e.position.end=n(l)),p+=m[0].length;continue}let h=e.slice(1).search(/<|\n/);h===-1?h=e.length:h+=1;let g=n(l),_=e.slice(0,h);t(l,_);let v=f(),y=n(l),b=v[v.length-1];b?.type===`text`?(b.value+=_,b.position.end=y):v.push({type:`text`,value:_,position:{start:g,end:y}}),p+=h}for(let e of d)e.position.end=n(l);return u.position.end=n(l),u}function s(e){return e.startsWith(`=#`)?`#${e.slice(2)}`:e.startsWith(`=@`)||e.startsWith(`@=`)?`@${e.slice(2)}`:e}export{o as parseGw2Markup};