@mdgate/subtitle 0.5.3 → 0.5.5
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 +3 -2
- package/dist/index.js +2 -2
- package/package.json +8 -5
package/README.md
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
# @mdgate/subtitle
|
|
2
2
|
|
|
3
|
-
Convert
|
|
3
|
+
Convert subtitle and caption files to Markdown. Outputs GitHub-Flavored Markdown. Works in Node,
|
|
4
4
|
Edge, and browsers. No native addons.
|
|
5
5
|
|
|
6
|
-
Handles: `.srt`, `.vtt`, `.webvtt`
|
|
6
|
+
Handles: `.srt`, `.vtt`, `.webvtt`, `.ass`, `.ssa`, `.lrc`, `.sub` (MicroDVD), `.sbv`, `.ttml`,
|
|
7
|
+
`.jss`
|
|
7
8
|
|
|
8
9
|
## Usage
|
|
9
10
|
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import{ConvertError as
|
|
1
|
+
import{ConvertError as M}from"@mdgate/core";import{documentToMarkdown as Pn}from"@mdgate/document";import{fileExtension as Gn}from"@mdgate/utils";import{emptyDocument as Cn,PLAIN as wn}from"@mdgate/document";import{cleanText as vn,collapseWs as $n,decode as S,lines as In,trim as l}from"@mdgate/utils";import{heading as nn,PLAIN as tn,plain as en}from"@mdgate/document";import{cleanText as rn,collapseWs as on,trim as p}from"@mdgate/utils";var sn={bold:!1,italic:!0,strike:!1,code:!1},F=/^\s*\[[A-Za-z][^:\]]*:[^\]]*\]\s*$/,un=/^\s*\[(-?\d{1,3}:\d{2}(?::\d{2})?(?:[.,]\d{1,3})?)\](.*)$/,O=/^\s*\{(-?\d+)\}\{(\d*)\}(.*)$/,cn=/\{[^}]*\}/g,fn=/^\s*(\d{1,2}):(\d{2}):(\d{2})[.,](\d{1,3})\s*,\s*(\d{1,2}):(\d{2}):(\d{2})[.,](\d{1,3})\s*$/,R=/^\s*(\d{1,2}):(\d{2}):(\d{2})[.,](\d{1,2})\s+(\d{1,2}):(\d{2}):(\d{2})[.,](\d{1,2})\b(.*)$/,J=/^\s*@(\d+)\s+@(\d+)\b(.*)$/,ln=/^(?:[jJvV][cCmMtTbBlLrR]|[jJvV][lLrR]\d+|[nNcC][cC]|[pP]\d+)\s+/,an=/\{[^}]*\}/g,dn=/\\[BIUN]/g,_=/<p\b([^>]*)>([\s\S]*?)<\/p>/gi,mn=/\bbegin\s*=\s*["']([^"']+)["']/i,gn=/<(?:[\w.]+:)?title\b[^>]*>([\s\S]*?)<\/(?:[\w.]+:)?title>/i,pn=/<\d{1,2}:\d{2}(?::\d{2})?(?:[.,]\d{1,3})?>/g,hn=/^\[(-?\d{1,3}:\d{2}(?::\d{2})?(?:[.,]\d{1,3})?)\]/,xn=/&(?:amp|lt|gt|quot|apos|nbsp|lrm|rlm|#\d+|#x[\da-f]+);/gi;function B(n){for(let t of n){let e=p(t);if(e.length===0)continue;if(F.test(e)||un.test(e))return!0;return!1}return!1}function P(n){for(let t of n){let e=p(t);if(e.length===0)continue;return O.test(e)}return!1}function G(n){for(let t of n){let e=p(t);if(e.length===0||e.startsWith("#"))continue;return R.test(e)||J.test(e)}return!1}function j(n){let t=n.slice(0,4096);if(!/<tt\b/i.test(t))return!1;return/ttml/i.test(t)||/<tt\b[^>]*xmlns/i.test(t)||/<p\b[^>]*\bbegin=/i.test(t)}function V(n,t){for(let e of n){let r=p(e);if(r.length===0||F.test(r))continue;let i=[],o=0;while(o<r.length){let u=hn.exec(r.slice(o));if(u===null)break;i.push(u[1]),o+=u[0].length}if(i.length===0)continue;let s=A(r.slice(o).replace(pn,""));if(s.length===0)continue;for(let u of i){let c=bn(u);if(c!==void 0)N(t,c,s)}}}function H(n,t){let e=25;for(let r of n){let i=O.exec(r);if(i===null)continue;let o=Number(i[1]),s=i[3]??"",u=/^\s*(\d+(?:\.\d+)?)\s*fps\s*$/i.exec(s);if(u!==null){let f=Number(u[1]);if(Number.isFinite(f)&&f>0)e=f;continue}let c=A(s.replace(cn,"").replace(/\|/g," ")),d=b(o/e);if(d===void 0||!Number.isFinite(o)||o<0||c.length===0)continue;N(t,d,c)}}function K(n,t){let e=100,r;for(let i of n){let o=r!==void 0?r+i.replace(/^\s+/,""):i;r=void 0;let s=p(o);if(s.length===0)continue;if(s.startsWith("#")){let m=/^#\s*timeres\s+(\d+)/i.exec(s);if(m!==null){let a=Number(m[1]);if(Number.isFinite(a)&&a>0)e=a}continue}if(/\\\s*$/.test(o)){r=o.replace(/\\\s*$/,"");continue}let u=R.exec(o);if(u!==null){let m=$(Number(u[1]),Number(u[2]),Number(u[3]),Number(u[4])*10),a=E(u[9]??"");if(m!==void 0&&a.length>0)N(t,m,a);continue}let c=J.exec(o);if(c===null)continue;let d=b(Number(c[1])/e),f=E(c[3]??"");if(d!==void 0&&f.length>0)N(t,d,f)}}function q(n,t){let e=gn.exec(n);if(e!==null){let i=A(e[1].replace(/<[^>]+>/g,""));if(i.length>0)t.blocks.push(nn(1,[en(i)]))}_.lastIndex=0;let r;while((r=_.exec(n))!==null){let i=mn.exec(r[1]??"");if(i===null)continue;let o=Nn(i[1]),s=A(r[2].replace(/<br\s*\/?>/gi," "));if(o===void 0||s.length===0)continue;N(t,o,s)}}function v(n){let t=fn.exec(n);if(t===null)return;return $(Number(t[1]),Number(t[2]),Number(t[3]),An(t[4]))}function E(n){let t=p(n).replace(ln,"");return A(t.replace(an,"").replace(/\\n/gi," ").replace(dn,""))}function bn(n){let t=n.replace(",",".").replace(/^-/,"").split(":");if(t.length===2){let e=Number(t[0]),r=Number(t[1]);if(!Number.isFinite(e+r))return;return b(e*60+r)}if(t.length===3)return $(Number(t[0]),Number(t[1]),Number(t[2]),void 0);return}function Nn(n){let t=p(n).replace(",","."),e=/^(?:(\d+):)?(\d{1,2}):(\d{2}(?:\.\d+)?)$/.exec(t);if(e!==null){let i=e[1]!==void 0?Number(e[1]):0,o=Number(e[2]),s=Number(e[3]);if(!Number.isFinite(i+o+s))return;return b(i*3600+o*60+s)}let r=/^(\d+(?:\.\d+)?)s$/i.exec(t);if(r!==null)return b(Number(r[1]));return}function $(n,t,e,r){let i=Number.isFinite(e)&&e>=60?NaN:e,o=r===void 0?Math.round(e%1*1000):r,s=r===void 0?Math.floor(e):i;if(!Number.isFinite(n+t+s+o))return;return`${g(n,2)}:${g(t,2)}:${g(s,2)}.${g(o,3)}`}function b(n){if(!Number.isFinite(n)||n<0)return;let t=Math.round(n*1000),e=Math.floor(t/3600000),r=Math.floor(t%3600000/60000),i=Math.floor(t%60000/1000);return`${g(e,2)}:${g(r,2)}:${g(i,2)}.${g(t%1000,3)}`}function An(n){if(n.length===1)return Number(n)*100;if(n.length===2)return Number(n)*10;return Number(n.slice(0,3))}function N(n,t,e){n.blocks.push({type:"paragraph",inlines:Tn(t,e)})}function Tn(n,t){return[{type:"text",text:`[${n}]`,style:sn},{type:"text",text:` ${t}`,style:tn}]}function A(n){return p(on(rn(I(n.replace(/<[^>]+>/g,"")))))}function I(n){return n.replace(xn,(t)=>{let e=t.toLowerCase();switch(e){case"&":return"&";case"<":return"<";case">":return">";case""":return'"';case"'":return"'";case" ":return" ";case"‎":case"‏":return"";default:break}if(e.startsWith("&#x")){let r=Number.parseInt(e.slice(3,-1),16);return Number.isFinite(r)?String.fromCodePoint(r):""}if(e.startsWith("&#")){let r=Number.parseInt(e.slice(2,-1),10);return Number.isFinite(r)?String.fromCodePoint(r):""}return""})}function g(n,t){return n.toString().padStart(t,"0")}var Sn={bold:!1,italic:!0,strike:!1,code:!1},Ln=/^\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/,Mn=/\{[^}]*\}/g,Dn=/\\an\d+/gi,Un=/<[^>]*>/g;function z(n){let t=Bn(Jn(n)).replace(/\r\n/g,`
|
|
2
2
|
`).replace(/\r/g,`
|
|
3
|
-
`),e=
|
|
3
|
+
`),e=In(t),r=Cn();if(kn(e))return _n(e,r),r;if(j(t))return q(t,r),r;if(B(e))return V(e,r),r;if(P(e))return H(e,r),r;if(G(e))return K(e,r),r;let i=0;if(Wn(e[0])){i=1;while(i<e.length&&l(e[i]).length>0){if(x(e[i])!==void 0)break;if(i+1<e.length&&x(e[i+1])!==void 0)break;i+=1}}while(i<e.length){let o=e[i];if(l(o).length===0){i+=1;continue}if(On(o)){i+=1;while(i<e.length&&l(e[i]).length>0)i+=1;continue}let s=x(o)??v(o);if(s===void 0&&i+1<e.length){if(s=x(e[i+1])??v(e[i+1]),s!==void 0)i+=1}if(s===void 0){i+=1;while(i<e.length&&l(e[i]).length>0)i+=1;continue}i+=1;let u=[];while(i<e.length&&l(e[i]).length>0){if(x(e[i])!==void 0)break;if(i+1<e.length&&x(e[i+1])!==void 0)break;u.push(e[i]),i+=1}let c=Y(u.join(" "));if(c.length===0)continue;r.blocks.push({type:"paragraph",inlines:X(s,c)})}return r}function X(n,t){return[{type:"text",text:`[${n}]`,style:Sn},{type:"text",text:` ${t}`,style:wn}]}function x(n){let t=Ln.exec(n);if(t===null)return;let e=t[1]!==void 0?Number(t[1]):0,r=Number(t[2]),i=Number(t[3]),o=Number(t[4]);if(!Number.isFinite(e+r+i+o))return;return`${h(e,2)}:${h(r,2)}:${h(i,2)}.${h(o,3)}`}function Wn(n){if(n===void 0)return!1;return/^WEBVTT(?:$|[\s\uFEFF])/.test(n)}function kn(n){for(let t of n){let e=l(t);if(e.length===0)continue;let r=e.toLowerCase();return r==="[script info]"||r==="[v4+ styles]"||r==="[v4 styles]"||r==="[events]"}return!1}function _n(n,t){let e=!1,r=1,i=9;for(let o of n){let s=l(o);if(s.length===0)continue;if(s.startsWith("[")&&s.endsWith("]")){e=s.toLowerCase()==="[events]";continue}if(!e)continue;let u=s.indexOf(":");if(u<=0)continue;let c=s.slice(0,u).trim().toLowerCase(),d=s.slice(u+1).trim();if(c==="format"){let k=d.split(",").map((y)=>l(y).toLowerCase());if(r=k.indexOf("start"),i=k.indexOf("text"),r<0)r=1;continue}if(c!=="dialogue")continue;let f=En(d,Math.max(r,i)+1),m=Fn(f[r]??""),a=Y((f[i>=0?i:f.length-1]??"").replace(/\\N/g," "));if(m===void 0||a.length===0)continue;t.blocks.push({type:"paragraph",inlines:X(m,a)})}}function En(n,t){let e=[],r=0;for(let i=0;i<n.length;i+=1){if(n[i]!==",")continue;if(e.push(n.slice(r,i)),r=i+1,e.length+1>=t&&t>0)break}return e.push(n.slice(r)),e}function Fn(n){let t=/^\s*(\d+):(\d{1,2}):(\d{2})\.(\d{1,2})\s*$/.exec(n);if(t===null)return;let e=Number(t[1]),r=Number(t[2]),i=Number(t[3]),o=Number(t[4]);if(!Number.isFinite(e+r+i+o))return;return`${h(e,2)}:${h(r,2)}:${h(i,2)}.${h(o*10,3)}`}function On(n){let t=l(n);return L(t,"NOTE")||L(t,"STYLE")||L(t,"REGION")}function L(n,t){if(n.length<t.length)return!1;if(n.slice(0,t.length).toUpperCase()!==t)return!1;return n.length===t.length||Rn(n.charCodeAt(t.length))}function Rn(n){return n===9||n===10||n===13||n===32}function Y(n){let t=n.replace(Mn,"").replace(Dn,"").replace(Un,"");return l($n(vn(I(t))))}function Jn(n){if(n.length>=2&&n[0]===255&&n[1]===254)return S(n,"utf-16le");if(n.length>=2&&n[0]===254&&n[1]===255)return S(n,"utf-16be");let t=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(t)}catch{return S(t,"windows-1252")}}function Bn(n){return n.charCodeAt(0)===65279?n.slice(1):n}function h(n,t){return n.toString().padStart(t,"0")}var jn=new Set(["srt","vtt","webvtt","ass","ssa","lrc","sub","sbv","ttml","jss","jacosub"]);function D(){return{id:"subtitle",sniff(n,t){if(Hn(n))return 3;if(Vn(n))return 2;if(Kn(n))return 2;if(qn(n))return 2;if(zn(n))return 2;if(Xn(n))return 2;if(Yn(n))return 2;if(t?.path!==void 0&&jn.has(Gn(t.path)??""))return 1;return 0},convert(n){return Zn(n),{markdown:Pn(z(n))}}}}function Vn(n){let t=W(n);if(!Q(n,"WEBVTT",t))return!1;return!tt(n[t+6])}function Hn(n){let t=w(n);if(T(n,"[script info]",t))return!0;if(T(n,"[v4+ styles]",t))return!0;if(T(n,"[v4 styles]",t))return!0;return T(n,"[events]",t)}function Kn(n){let t=U(n);if(!/<tt\b/i.test(t))return!1;return/ttml/i.test(t)||/<tt\b[^>]*xmlns/i.test(t)||/<p\b[^>]*\bbegin=/i.test(t)}function qn(n){let t=w(n);if(n[t]!==91)return!1;if(t+=1,n[t]===45)t+=1;if(C(n[t])){while(C(n[t]))t+=1;return n[t]===58}if(!Z(n[t]))return!1;while(Z(n[t])||n[t]===95)t+=1;return n[t]===58}function zn(n){let t=w(n);if(n[t]!==123)return!1;if(t+=1,!C(n[t]))return!1;while(C(n[t]))t+=1;if(n[t]!==125||n[t+1]!==123)return!1;return!0}function Xn(n){return/^\s*\d{1,2}:\d{2}:\d{2}[.,]\d{1,3}\s*,\s*\d{1,2}:\d{2}:\d{2}[.,]\d{1,3}\b/m.test(U(n))}function Yn(n){let t=U(n);for(let e of t.split(/\r?\n/)){let r=e.trim();if(r.length===0||r.startsWith("#"))continue;return/^\d{1,2}:\d{2}:\d{2}[.,]\d{1,2}\s+\d{1,2}:\d{2}:\d{2}[.,]\d{1,2}\b/.test(r)||/^@\d+\s+@\d+\b/.test(r)}return!1}function U(n){let t=W(n);return new TextDecoder("utf-8",{fatal:!1}).decode(n.subarray(t,Math.min(n.length,t+4096)))}function C(n){return n!==void 0&&n>=48&&n<=57}function Z(n){return n!==void 0&&(n>=65&&n<=90||n>=97&&n<=122)}function T(n,t,e){if(e+t.length>n.length)return!1;for(let r=0;r<t.length;r+=1){let i=n[e+r],o=t.charCodeAt(r);if(i===o)continue;let s=i>=65&&i<=90?i+32:i,u=o>=65&&o<=90?o+32:o;if(s!==u)return!1}return!0}function Zn(n){if(Qn(n))throw M.unsupported("pdf");if(yn(n))throw M.unsupported("ole");if(nt(n))throw M.unsupported("zip")}function Qn(n){let t=w(n);return Q(n,"%PDF-",t)}function yn(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 nt(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 W(n){if(n.length>=3&&n[0]===239&&n[1]===187&&n[2]===191)return 3;return 0}function w(n){let t=W(n);while(t<n.length){let e=n[t];if(e!==9&&e!==10&&e!==13&&e!==32)break;t+=1}return t}function Q(n,t,e){if(e+t.length>n.length)return!1;for(let r=0;r<t.length;r+=1)if(n[e+r]!==t.charCodeAt(r))return!1;return!0}function tt(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 et}from"@mdgate/core";var rt=et([D()]);function it(n,t){return rt(n,t)}export{it as toMarkdown,D as subtitle};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mdgate/subtitle",
|
|
3
|
-
"version": "0.5.
|
|
4
|
-
"description": "mdgate
|
|
3
|
+
"version": "0.5.5",
|
|
4
|
+
"description": "mdgate subtitle and caption converter",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"sideEffects": false,
|
|
@@ -22,9 +22,9 @@
|
|
|
22
22
|
"prepublishOnly": "bun run build"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@mdgate/core": "0.5.
|
|
26
|
-
"@mdgate/document": "0.5.
|
|
27
|
-
"@mdgate/utils": "0.5.
|
|
25
|
+
"@mdgate/core": "0.5.5",
|
|
26
|
+
"@mdgate/document": "0.5.5",
|
|
27
|
+
"@mdgate/utils": "0.5.5"
|
|
28
28
|
},
|
|
29
29
|
"publishConfig": {
|
|
30
30
|
"access": "public"
|
|
@@ -38,6 +38,9 @@
|
|
|
38
38
|
"srt",
|
|
39
39
|
"vtt",
|
|
40
40
|
"webvtt",
|
|
41
|
+
"ass",
|
|
42
|
+
"lrc",
|
|
43
|
+
"ttml",
|
|
41
44
|
"subtitle"
|
|
42
45
|
]
|
|
43
46
|
}
|