@bscotch/steam-bbcode 0.2.0 → 0.3.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/README.md +18 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -87
- package/dist/index.js.map +1 -1
- package/dist/lib.d.ts +3 -0
- package/dist/lib.d.ts.map +1 -0
- package/dist/lib.js +88 -0
- package/dist/lib.js.map +1 -0
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -22,6 +22,8 @@ Note: currently only supports _pure_ Markdown (embedded HTML is left as-is).
|
|
|
22
22
|
|
|
23
23
|
## Usage
|
|
24
24
|
|
|
25
|
+
For a typical use case:
|
|
26
|
+
|
|
25
27
|
```ts
|
|
26
28
|
import { md2bbcode } from '@bscotch/steam-bbcode';
|
|
27
29
|
|
|
@@ -29,6 +31,22 @@ const converted = md2bbcode('# Hello, world!');
|
|
|
29
31
|
const bbcode = converted.bbcode;
|
|
30
32
|
```
|
|
31
33
|
|
|
34
|
+
This package uses [marked](https://www.npmjs.com/package/marked) for rendering. If you want to use your own version of the marked dependency, or want to further modify rendering with your own marked extensions, you can import the BBCode marked extension from this package to use directly:
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { markedBbcodeExtension } from '@bscotch/steam-bbcode/lib';
|
|
38
|
+
import { Marked } from 'marked';
|
|
39
|
+
|
|
40
|
+
const marked = new Marked(
|
|
41
|
+
markedBbcodeExtension /* ... optional other extensions */,
|
|
42
|
+
);
|
|
43
|
+
function toBbcode(md: string): string {
|
|
44
|
+
// (Marked replaces single-quotes with ', which Steam doesn't know what to do with.)
|
|
45
|
+
return (marked.parse(source) as string).replaceAll(''', "'");
|
|
46
|
+
}
|
|
47
|
+
const converted = toBbcode('# Hello, world!');
|
|
48
|
+
```
|
|
49
|
+
|
|
32
50
|
## Example
|
|
33
51
|
|
|
34
52
|
The following Markdown input:
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAIjD;;GAEG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG;IACzC,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;CAEhB,CAMA"}
|
package/dist/index.js
CHANGED
|
@@ -1,83 +1,7 @@
|
|
|
1
1
|
import { Marked } from 'marked';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
// Steam only supports h1-h3, the rest should just be bold.
|
|
6
|
-
const text = this.parser.parseInline(src.tokens);
|
|
7
|
-
if (src.depth <= 3) {
|
|
8
|
-
return `[h${src.depth}]${text}[/h${src.depth}]\n\n`;
|
|
9
|
-
}
|
|
10
|
-
else {
|
|
11
|
-
return `[b]${text}[/b]\n\n`;
|
|
12
|
-
}
|
|
13
|
-
},
|
|
14
|
-
image(src) {
|
|
15
|
-
return bbImage(src.href, src.text);
|
|
16
|
-
},
|
|
17
|
-
link(src) {
|
|
18
|
-
return `[url=${src.href}]${this.parser.parseInline(src.tokens)}[/url]`;
|
|
19
|
-
},
|
|
20
|
-
em(src) {
|
|
21
|
-
return `[i]${this.parser.parseInline(src.tokens)}[/i]`;
|
|
22
|
-
},
|
|
23
|
-
strong(src) {
|
|
24
|
-
return `[b]${this.parser.parseInline(src.tokens)}[/b]`;
|
|
25
|
-
},
|
|
26
|
-
del(src) {
|
|
27
|
-
return `[strike]${this.parser.parseInline(src.tokens)}[/strike]`;
|
|
28
|
-
},
|
|
29
|
-
codespan(src) {
|
|
30
|
-
return `[code]${src.text}[/code]`;
|
|
31
|
-
},
|
|
32
|
-
code(src) {
|
|
33
|
-
return `[code]\n${src.text}\n[/code]\n\n`;
|
|
34
|
-
},
|
|
35
|
-
paragraph(src) {
|
|
36
|
-
// For lines that are just URLs,
|
|
37
|
-
// if they are steam or youtube links, leave them as-is
|
|
38
|
-
if (src.tokens.length === 1 &&
|
|
39
|
-
src.tokens[0].type === 'link' &&
|
|
40
|
-
src.tokens[0].raw.match(/^(https?:\/\/)(www\.)?(store\.steampowered\.com|steamcommunity\.com|youtube\.com|youtu\.be)/)) {
|
|
41
|
-
return src.tokens[0].raw + '\n\n';
|
|
42
|
-
}
|
|
43
|
-
return this.parser.parseInline(src.tokens) + '\n\n';
|
|
44
|
-
},
|
|
45
|
-
listitem(src) {
|
|
46
|
-
return ` [*] ${this.parser.parseInline(src.tokens).trim()}`;
|
|
47
|
-
},
|
|
48
|
-
list(src) {
|
|
49
|
-
const el = src.ordered ? 'olist' : 'list';
|
|
50
|
-
let indent = ' ';
|
|
51
|
-
let rendered = `[${el}]\n`;
|
|
52
|
-
for (const item of src.items) {
|
|
53
|
-
rendered += `${indent}[*] ${this.parser.parseInline(item.tokens).trim()}\n`;
|
|
54
|
-
}
|
|
55
|
-
return `${rendered}[/${el}]\n\n`;
|
|
56
|
-
},
|
|
57
|
-
hr() {
|
|
58
|
-
return '[hr][/hr]\n\n';
|
|
59
|
-
},
|
|
60
|
-
br() {
|
|
61
|
-
return '\n\n';
|
|
62
|
-
},
|
|
63
|
-
blockquote(src) {
|
|
64
|
-
return `[quote]${this.parser.parse(src.tokens).trim()}[/quote]\n\n`;
|
|
65
|
-
},
|
|
66
|
-
html(src) {
|
|
67
|
-
if (src.raw.startsWith('<img')) {
|
|
68
|
-
const alt = src.raw.match(/alt="([^"]+)"/)?.[1] || '';
|
|
69
|
-
const source = src.raw.match(/src="([^"]+)"/)?.[1] || '';
|
|
70
|
-
return bbImage(source, alt);
|
|
71
|
-
}
|
|
72
|
-
else if (src.raw.startsWith('<!-')) {
|
|
73
|
-
// Remove HTML comments
|
|
74
|
-
return '';
|
|
75
|
-
}
|
|
76
|
-
// Otherwise return as-is for manual editing
|
|
77
|
-
return src.text;
|
|
78
|
-
},
|
|
79
|
-
},
|
|
80
|
-
});
|
|
2
|
+
import { markedBbcodeExtension } from './lib.js';
|
|
3
|
+
export { markedBbcodeExtension } from './lib.js';
|
|
4
|
+
const marked = new Marked(markedBbcodeExtension);
|
|
81
5
|
/**
|
|
82
6
|
* Convert Markdown (or HTML) to Steam-compatible BBCode. Also returns the HTML content.
|
|
83
7
|
*/
|
|
@@ -88,12 +12,4 @@ export function md2bbcode(source) {
|
|
|
88
12
|
bbcode,
|
|
89
13
|
};
|
|
90
14
|
}
|
|
91
|
-
function bbImage(src, alt) {
|
|
92
|
-
let bb = '[img';
|
|
93
|
-
if (alt) {
|
|
94
|
-
bb += ` alt="${alt.replaceAll('"', '"')}"`;
|
|
95
|
-
}
|
|
96
|
-
bb += `]${src}[/img]`;
|
|
97
|
-
return bb;
|
|
98
|
-
}
|
|
99
15
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AACjD,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAEjD,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,qBAAqB,CAAC,CAAC;AAEjD;;GAEG;AACH,MAAM,UAAU,SAAS,CAAC,MAAc;IAKtC,MAAM,MAAM,GAAI,MAAM,CAAC,KAAK,CAAC,MAAM,CAAY,CAAC,UAAU,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;IACzE,OAAO;QACL,MAAM;QACN,MAAM;KACP,CAAC;AACJ,CAAC"}
|
package/dist/lib.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lib.d.ts","sourceRoot":"","sources":["../src/lib.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AAE9C,eAAO,MAAM,qBAAqB,EAAE,eAgFnC,CAAC"}
|
package/dist/lib.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
export const markedBbcodeExtension = {
|
|
2
|
+
renderer: {
|
|
3
|
+
heading(src) {
|
|
4
|
+
// Steam only supports h1-h3, the rest should just be bold.
|
|
5
|
+
const text = this.parser.parseInline(src.tokens);
|
|
6
|
+
if (src.depth <= 3) {
|
|
7
|
+
return `[h${src.depth}]${text}[/h${src.depth}]\n\n`;
|
|
8
|
+
}
|
|
9
|
+
else {
|
|
10
|
+
return `[b]${text}[/b]\n\n`;
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
image(src) {
|
|
14
|
+
return bbImage(src.href, src.text);
|
|
15
|
+
},
|
|
16
|
+
link(src) {
|
|
17
|
+
return `[url=${src.href}]${this.parser.parseInline(src.tokens)}[/url]`;
|
|
18
|
+
},
|
|
19
|
+
em(src) {
|
|
20
|
+
return `[i]${this.parser.parseInline(src.tokens)}[/i]`;
|
|
21
|
+
},
|
|
22
|
+
strong(src) {
|
|
23
|
+
return `[b]${this.parser.parseInline(src.tokens)}[/b]`;
|
|
24
|
+
},
|
|
25
|
+
del(src) {
|
|
26
|
+
return `[strike]${this.parser.parseInline(src.tokens)}[/strike]`;
|
|
27
|
+
},
|
|
28
|
+
codespan(src) {
|
|
29
|
+
return `[code]${src.text}[/code]`;
|
|
30
|
+
},
|
|
31
|
+
code(src) {
|
|
32
|
+
return `[code]\n${src.text}\n[/code]\n\n`;
|
|
33
|
+
},
|
|
34
|
+
paragraph(src) {
|
|
35
|
+
// For lines that are just URLs,
|
|
36
|
+
// if they are steam or youtube links, leave them as-is
|
|
37
|
+
if (src.tokens.length === 1 &&
|
|
38
|
+
src.tokens[0].type === 'link' &&
|
|
39
|
+
src.tokens[0].raw.match(/^(https?:\/\/)(www\.)?(store\.steampowered\.com|steamcommunity\.com|youtube\.com|youtu\.be)/)) {
|
|
40
|
+
return src.tokens[0].raw + '\n\n';
|
|
41
|
+
}
|
|
42
|
+
return this.parser.parseInline(src.tokens) + '\n\n';
|
|
43
|
+
},
|
|
44
|
+
listitem(src) {
|
|
45
|
+
return ` [*] ${this.parser.parseInline(src.tokens).trim()}`;
|
|
46
|
+
},
|
|
47
|
+
list(src) {
|
|
48
|
+
const el = src.ordered ? 'olist' : 'list';
|
|
49
|
+
let indent = ' ';
|
|
50
|
+
let rendered = `[${el}]\n`;
|
|
51
|
+
for (const item of src.items) {
|
|
52
|
+
rendered += `${indent}[*] ${this.parser.parseInline(item.tokens).trim()}\n`;
|
|
53
|
+
}
|
|
54
|
+
return `${rendered}[/${el}]\n\n`;
|
|
55
|
+
},
|
|
56
|
+
hr() {
|
|
57
|
+
return '[hr][/hr]\n\n';
|
|
58
|
+
},
|
|
59
|
+
br() {
|
|
60
|
+
return '\n\n';
|
|
61
|
+
},
|
|
62
|
+
blockquote(src) {
|
|
63
|
+
return `[quote]${this.parser.parse(src.tokens).trim()}[/quote]\n\n`;
|
|
64
|
+
},
|
|
65
|
+
html(src) {
|
|
66
|
+
if (src.raw.startsWith('<img')) {
|
|
67
|
+
const alt = src.raw.match(/alt="([^"]+)"/)?.[1] || '';
|
|
68
|
+
const source = src.raw.match(/src="([^"]+)"/)?.[1] || '';
|
|
69
|
+
return bbImage(source, alt);
|
|
70
|
+
}
|
|
71
|
+
else if (src.raw.startsWith('<!-')) {
|
|
72
|
+
// Remove HTML comments
|
|
73
|
+
return '';
|
|
74
|
+
}
|
|
75
|
+
// Otherwise return as-is for manual editing
|
|
76
|
+
return src.text;
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
function bbImage(src, alt) {
|
|
81
|
+
let bb = '[img';
|
|
82
|
+
if (alt) {
|
|
83
|
+
bb += ` alt="${alt.replaceAll('"', '"')}"`;
|
|
84
|
+
}
|
|
85
|
+
bb += `]${src}[/img]`;
|
|
86
|
+
return bb;
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=lib.js.map
|
package/dist/lib.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lib.js","sourceRoot":"","sources":["../src/lib.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,qBAAqB,GAAoB;IACpD,QAAQ,EAAE;QACR,OAAO,CAAC,GAAG;YACT,2DAA2D;YAC3D,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;YACjD,IAAI,GAAG,CAAC,KAAK,IAAI,CAAC,EAAE;gBAClB,OAAO,KAAK,GAAG,CAAC,KAAK,IAAI,IAAI,MAAM,GAAG,CAAC,KAAK,OAAO,CAAC;aACrD;iBAAM;gBACL,OAAO,MAAM,IAAI,UAAU,CAAC;aAC7B;QACH,CAAC;QACD,KAAK,CAAC,GAAG;YACP,OAAO,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,IAAI,CAAC,CAAC;QACrC,CAAC;QACD,IAAI,CAAC,GAAG;YACN,OAAO,QAAQ,GAAG,CAAC,IAAI,IAAI,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC;QACzE,CAAC;QACD,EAAE,CAAC,GAAG;YACJ,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC;QACzD,CAAC;QACD,MAAM,CAAC,GAAG;YACR,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,GAAG,CAAC,MAAM,CAAC,MAAM,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
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bscotch/steam-bbcode",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Generate Steam-compatible BBCode from Markdown.",
|
|
5
5
|
"homepage": "https://github.com/bscotch/stitch/tree/develop/packages/steam-bbcode#readme",
|
|
6
6
|
"repository": {
|
|
@@ -13,6 +13,10 @@
|
|
|
13
13
|
".": {
|
|
14
14
|
"types": "./dist/index.d.ts",
|
|
15
15
|
"import": "./dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./lib": {
|
|
18
|
+
"types": "./dist/lib.d.ts",
|
|
19
|
+
"import": "./dist/lib.js"
|
|
16
20
|
}
|
|
17
21
|
},
|
|
18
22
|
"main": "dist/index.js",
|