@mdgate/subtitle 0.5.0 → 0.5.2
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/dist/index.js +2 -2
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import{ConvertError as
|
|
1
|
+
import{ConvertError as h}from"@mdgate/core";import{documentToMarkdown as j}from"@mdgate/document";import{fileExtension as J}from"@mdgate/utils";import{emptyDocument as E,PLAIN as $}from"@mdgate/document";import{cleanText as F,collapseWs as b,decode as p,lines as L,trim as s}from"@mdgate/utils";var B={bold:!1,italic:!0,strike:!1,code:!1},D=/^\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/,O=/\{[^}]*\}/g,P=/\\an\d+/gi,G=/<[^>]*>/g,M=/&(?:amp|lt|gt|quot|apos|nbsp|lrm|rlm|#\d+|#x[\da-f]+);/gi;function T(n){let t=Z(K(n)).replace(/\r\n/g,`
|
|
2
2
|
`).replace(/\r/g,`
|
|
3
|
-
`),
|
|
3
|
+
`),e=L(t),i=E();if(H(e))return _(e,i),i;let r=0;if(R(e[0])){r=1;while(r<e.length&&s(e[r]).length>0){if(a(e[r])!==void 0)break;if(r+1<e.length&&a(e[r+1])!==void 0)break;r+=1}}while(r<e.length){let o=e[r];if(s(o).length===0){r+=1;continue}if(X(o)){r+=1;while(r<e.length&&s(e[r]).length>0)r+=1;continue}let u=a(o);if(u===void 0&&r+1<e.length){if(u=a(e[r+1]),u!==void 0)r+=1}if(u===void 0){r+=1;while(r<e.length&&s(e[r]).length>0)r+=1;continue}r+=1;let f=[];while(r<e.length&&s(e[r]).length>0){if(a(e[r])!==void 0)break;if(r+1<e.length&&a(e[r+1])!==void 0)break;f.push(e[r]),r+=1}let l=k(f.join(" "));if(l.length===0)continue;i.blocks.push({type:"paragraph",inlines:v(u,l)})}return i}function v(n,t){return[{type:"text",text:`[${n}]`,style:B},{type:"text",text:` ${t}`,style:$}]}function a(n){let t=D.exec(n);if(t===null)return;let e=t[1]!==void 0?Number(t[1]):0,i=Number(t[2]),r=Number(t[3]),o=Number(t[4]);if(!Number.isFinite(e+i+r+o))return;return`${c(e,2)}:${c(i,2)}:${c(r,2)}.${c(o,3)}`}function R(n){if(n===void 0)return!1;return/^WEBVTT(?:$|[\s\uFEFF])/.test(n)}function H(n){for(let t of n){let e=s(t);if(e.length===0)continue;let i=e.toLowerCase();return i==="[script info]"||i==="[v4+ styles]"||i==="[v4 styles]"||i==="[events]"}return!1}function _(n,t){let e=!1,i=1,r=9;for(let o of n){let u=s(o);if(u.length===0)continue;if(u.startsWith("[")&&u.endsWith("]")){e=u.toLowerCase()==="[events]";continue}if(!e)continue;let f=u.indexOf(":");if(f<=0)continue;let l=u.slice(0,f).trim().toLowerCase(),A=u.slice(f+1).trim();if(l==="format"){let C=A.split(",").map((I)=>s(I).toLowerCase());if(i=C.indexOf("start"),r=C.indexOf("text"),i<0)i=1;continue}if(l!=="dialogue")continue;let g=q(A,Math.max(i,r)+1),w=V(g[i]??""),N=k((g[r>=0?r:g.length-1]??"").replace(/\\N/g," "));if(w===void 0||N.length===0)continue;t.blocks.push({type:"paragraph",inlines:v(w,N)})}}function q(n,t){let e=[],i=0;for(let r=0;r<n.length;r+=1){if(n[r]!==",")continue;if(e.push(n.slice(i,r)),i=r+1,e.length+1>=t&&t>0)break}return e.push(n.slice(i)),e}function V(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]),i=Number(t[2]),r=Number(t[3]),o=Number(t[4]);if(!Number.isFinite(e+i+r+o))return;return`${c(e,2)}:${c(i,2)}:${c(r,2)}.${c(o*10,3)}`}function X(n){let t=s(n);return m(t,"NOTE")||m(t,"STYLE")||m(t,"REGION")}function m(n,t){if(n.length<t.length)return!1;if(n.slice(0,t.length).toUpperCase()!==t)return!1;return n.length===t.length||Y(n.charCodeAt(t.length))}function Y(n){return n===9||n===10||n===13||n===32}function k(n){let t=n.replace(O,"").replace(P,"").replace(G,"");return s(b(F(z(t))))}function z(n){return n.replace(M,(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 i=Number.parseInt(e.slice(3,-1),16);return Number.isFinite(i)?String.fromCodePoint(i):""}if(e.startsWith("&#")){let i=Number.parseInt(e.slice(2,-1),10);return Number.isFinite(i)?String.fromCodePoint(i):""}return""})}function K(n){if(n.length>=2&&n[0]===255&&n[1]===254)return p(n,"utf-16le");if(n.length>=2&&n[0]===254&&n[1]===255)return p(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 p(t,"windows-1252")}}function Z(n){return n.charCodeAt(0)===65279?n.slice(1):n}function c(n,t){return n.toString().padStart(t,"0")}var Q=new Set(["srt","vtt","webvtt","ass","ssa"]);function x(){return{id:"subtitle",sniff(n,t){if(nn(n))return 3;if(y(n))return 2;if(t?.path!==void 0&&Q.has(J(t.path)??""))return 1;return 0},convert(n){return tn(n),{markdown:j(T(n))}}}}function y(n){let t=U(n);if(!W(n,"WEBVTT",t))return!1;return!un(n[t+6])}function nn(n){let t=S(n);if(d(n,"[script info]",t))return!0;if(d(n,"[v4+ styles]",t))return!0;if(d(n,"[v4 styles]",t))return!0;return d(n,"[events]",t)}function d(n,t,e){if(e+t.length>n.length)return!1;for(let i=0;i<t.length;i+=1){let r=n[e+i],o=t.charCodeAt(i);if(r===o)continue;let u=r>=65&&r<=90?r+32:r,f=o>=65&&o<=90?o+32:o;if(u!==f)return!1}return!0}function tn(n){if(en(n))throw h.unsupported("pdf");if(rn(n))throw h.unsupported("ole");if(on(n))throw h.unsupported("zip")}function en(n){let t=S(n);return W(n,"%PDF-",t)}function rn(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 on(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 U(n){if(n.length>=3&&n[0]===239&&n[1]===187&&n[2]===191)return 3;return 0}function S(n){let t=U(n);while(t<n.length){let e=n[t];if(e!==9&&e!==10&&e!==13&&e!==32)break;t+=1}return t}function W(n,t,e){if(e+t.length>n.length)return!1;for(let i=0;i<t.length;i+=1)if(n[e+i]!==t.charCodeAt(i))return!1;return!0}function un(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 sn}from"@mdgate/core";var fn=sn([x()]);function cn(n,t){return fn(n,t)}export{cn as toMarkdown,x as subtitle};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mdgate/subtitle",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "mdgate SRT and WebVTT converter",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -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.2",
|
|
26
|
+
"@mdgate/document": "0.5.2",
|
|
27
|
+
"@mdgate/utils": "0.5.2"
|
|
28
28
|
},
|
|
29
29
|
"publishConfig": {
|
|
30
30
|
"access": "public"
|