@mdgate/subtitle 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/README.md +26 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/subtitle.d.ts +2 -0
- package/dist/to-markdown.d.ts +6 -0
- package/package.json +43 -0
package/README.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# @mdgate/subtitle
|
|
2
|
+
|
|
3
|
+
Convert SRT and WebVTT subtitle files to Markdown. Outputs GitHub-Flavored Markdown. Works in Node,
|
|
4
|
+
Edge, and browsers. No native addons.
|
|
5
|
+
|
|
6
|
+
Handles: `.srt`, `.vtt`, `.webvtt`
|
|
7
|
+
|
|
8
|
+
## Usage
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import { toMarkdown } from '@mdgate/subtitle';
|
|
12
|
+
|
|
13
|
+
const markdown = await toMarkdown(bytes);
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Compose with other converters:
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { create } from '@mdgate/core';
|
|
20
|
+
import { subtitle } from '@mdgate/subtitle';
|
|
21
|
+
|
|
22
|
+
const convert = create([subtitle()]);
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Part of [mdgate converters](https://github.com/mdgate/converters); install
|
|
26
|
+
`@mdgate/converters` for every format at once.
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import{ConvertError as d}from"@mdgate/core";import{documentToMarkdown as H}from"@mdgate/document";import{fileExtension as M}from"@mdgate/utils";import{emptyDocument as T,PLAIN as N}from"@mdgate/document";import{cleanText as w,collapseWs as C,decode as c,lines as S,trim as o}from"@mdgate/utils";var U={bold:!1,italic:!0,strike:!1,code:!1},k=/^\s*(?:(\d{1,3}):)?(\d{1,2}):(\d{2})[,.](\d{1,3})\s*-->\s*(?:(\d{1,3}):)?(\d{1,2}):(\d{2})[,.](\d{1,3})\b/,I=/\{[^}]*\}/g,E=/\\an\d+/gi,W=/<[^>]*>/g,v=/&(?:amp|lt|gt|quot|apos|nbsp|lrm|rlm|#\d+|#x[\da-f]+);/gi;function x(n){let e=R(L(n)).replace(/\r\n/g,`
|
|
2
|
+
`).replace(/\r/g,`
|
|
3
|
+
`),r=S(e),i=T(),t=0;if(B(r[0])){t=1;while(t<r.length&&o(r[t]).length>0){if(f(r[t])!==void 0)break;if(t+1<r.length&&f(r[t+1])!==void 0)break;t+=1}}while(t<r.length){let u=r[t];if(o(u).length===0){t+=1;continue}if($(u)){t+=1;while(t<r.length&&o(r[t]).length>0)t+=1;continue}let a=f(u);if(a===void 0&&t+1<r.length){if(a=f(r[t+1]),a!==void 0)t+=1}if(a===void 0){t+=1;while(t<r.length&&o(r[t]).length>0)t+=1;continue}t+=1;let g=[];while(t<r.length&&o(r[t]).length>0){if(f(r[t])!==void 0)break;if(t+1<r.length&&f(r[t+1])!==void 0)break;g.push(r[t]),t+=1}let m=D(g.join(" "));if(m.length===0)continue;i.blocks.push({type:"paragraph",inlines:F(a,m)})}return i}function F(n,e){return[{type:"text",text:`[${n}]`,style:U},{type:"text",text:` ${e}`,style:N}]}function f(n){let e=k.exec(n);if(e===null)return;let r=e[1]!==void 0?Number(e[1]):0,i=Number(e[2]),t=Number(e[3]),u=Number(e[4]);if(!Number.isFinite(r+i+t+u))return;return`${l(r,2)}:${l(i,2)}:${l(t,2)}.${l(u,3)}`}function B(n){if(n===void 0)return!1;return/^WEBVTT(?:$|[\s\uFEFF])/.test(n)}function $(n){let e=o(n);return s(e,"NOTE")||s(e,"STYLE")||s(e,"REGION")}function s(n,e){if(n.length<e.length)return!1;if(n.slice(0,e.length).toUpperCase()!==e)return!1;return n.length===e.length||P(n.charCodeAt(e.length))}function P(n){return n===9||n===10||n===13||n===32}function D(n){let e=n.replace(I,"").replace(E,"").replace(W,"");return o(C(w(G(e))))}function G(n){return n.replace(v,(e)=>{let r=e.toLowerCase();switch(r){case"&":return"&";case"<":return"<";case">":return">";case""":return'"';case"'":return"'";case" ":return" ";case"‎":case"‏":return"";default:break}if(r.startsWith("&#x")){let i=Number.parseInt(r.slice(3,-1),16);return Number.isFinite(i)?String.fromCodePoint(i):""}if(r.startsWith("&#")){let i=Number.parseInt(r.slice(2,-1),10);return Number.isFinite(i)?String.fromCodePoint(i):""}return""})}function L(n){if(n.length>=2&&n[0]===255&&n[1]===254)return c(n,"utf-16le");if(n.length>=2&&n[0]===254&&n[1]===255)return c(n,"utf-16be");let e=n.length>=3&&n[0]===239&&n[1]===187&&n[2]===191?n.subarray(3):n;try{return new TextDecoder("utf-8",{fatal:!0}).decode(e)}catch{return c(e,"windows-1252")}}function R(n){return n.charCodeAt(0)===65279?n.slice(1):n}function l(n,e){return n.toString().padStart(e,"0")}var O=new Set(["srt","vtt","webvtt"]);function p(){return{id:"subtitle",sniff(n,e){if(_(n))return 2;if(e?.path!==void 0&&O.has(M(e.path)??""))return 1;return 0},convert(n){return q(n),{markdown:H(x(n))}}}}function _(n){let e=h(n);if(!A(n,"WEBVTT",e))return!1;return!K(n[e+6])}function q(n){if(V(n))throw d.unsupported("pdf");if(X(n))throw d.unsupported("ole");if(Y(n))throw d.unsupported("zip")}function V(n){let e=z(n);return A(n,"%PDF-",e)}function X(n){return n.length>=8&&n[0]===208&&n[1]===207&&n[2]===17&&n[3]===224&&n[4]===161&&n[5]===177&&n[6]===26&&n[7]===225}function Y(n){if(n.length<4||n[0]!==80||n[1]!==75)return!1;return n[2]===3&&n[3]===4||n[2]===5&&n[3]===6||n[2]===7&&n[3]===8}function h(n){if(n.length>=3&&n[0]===239&&n[1]===187&&n[2]===191)return 3;return 0}function z(n){let e=h(n);while(e<n.length){let r=n[e];if(r!==9&&r!==10&&r!==13&&r!==32)break;e+=1}return e}function A(n,e,r){if(r+e.length>n.length)return!1;for(let i=0;i<e.length;i+=1)if(n[r+i]!==e.charCodeAt(i))return!1;return!0}function K(n){if(n===void 0)return!1;return n>=48&&n<=57||n>=65&&n<=90||n>=97&&n<=122||n===45||n===95||n===58}import{create as Z}from"@mdgate/core";var b=Z([p()]);function j(n,e){return b(n,e)}export{j as toMarkdown,p as subtitle};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type ConvertHint } from '@mdgate/core';
|
|
2
|
+
/**
|
|
3
|
+
* Convert a single file's bytes to Markdown with the subtitle converter.
|
|
4
|
+
* `hint.path` is only a sniff hint; it is never read from disk.
|
|
5
|
+
*/
|
|
6
|
+
export declare function toMarkdown(bytes: Uint8Array, hint?: ConvertHint): Promise<string>;
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mdgate/subtitle",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "mdgate SRT and WebVTT converter",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"files": [
|
|
17
|
+
"dist/**/*.js",
|
|
18
|
+
"dist/**/*.d.ts"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "bun ../../scripts/build-package.ts",
|
|
22
|
+
"prepublishOnly": "bun run build"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@mdgate/core": "0.1.3",
|
|
26
|
+
"@mdgate/document": "0.1.0",
|
|
27
|
+
"@mdgate/utils": "0.1.2"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=20"
|
|
34
|
+
},
|
|
35
|
+
"keywords": [
|
|
36
|
+
"mdgate",
|
|
37
|
+
"markdown",
|
|
38
|
+
"srt",
|
|
39
|
+
"vtt",
|
|
40
|
+
"webvtt",
|
|
41
|
+
"subtitle"
|
|
42
|
+
]
|
|
43
|
+
}
|