@takumi-rs/helpers 2.0.0-rc.1 → 2.0.0-rc.11

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 CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  **Utility functions and types for working with Takumi node trees.**
4
4
 
5
- This package provides the core logic for converting JSX and HTML into the layout-ready node trees that Takumi's Rust engine expects. It also handles resource fetching (fonts, images) and emoji processing.
5
+ Convert JSX and HTML into the node trees Takumi's Rust engine renders. Load fonts and process emoji.
6
6
 
7
7
  [Documentation](https://takumi.kane.tw/docs/architecture#fromjsx-helper) · [GitHub](https://github.com/kane50613/takumi)
8
8
 
@@ -34,17 +34,6 @@ import { fromHtml } from "@takumi-rs/helpers/html";
34
34
  const { node, stylesheets } = await fromHtml("<div style='color: red'>Hello</div>");
35
35
  ```
36
36
 
37
- ### Resource Fetching
38
-
39
- Extract and fetch external resources (images, fonts) mentioned in a node tree.
40
-
41
- ```ts
42
- import { extractResourceUrls, fetchResources } from "@takumi-rs/helpers";
43
-
44
- const urls = extractResourceUrls(node);
45
- const resources = await fetchResources(urls);
46
- ```
47
-
48
37
  ### Emoji Processing
49
38
 
50
39
  Find and replace emoji characters in text nodes with image nodes (Twemoji or custom).
@@ -0,0 +1 @@
1
+ const e=/url\(\s*(['"]?)(.*?)\1\s*\)/g;async function t(e,t={}){let n=t.fetch??globalThis.fetch,r=t.timeout===void 0?void 0:AbortSignal.timeout(t.timeout),i=[t.signal,t.init?.signal,r].filter(e=>e!==void 0),a=i.length?AbortSignal.any(i):void 0,o=await n(e,{...t.init,signal:a});if(!o.ok)throw Error(`HTTP ${o.status} ${o.statusText} fetching ${e}`);return o}function n(e){return e.startsWith(`https://`)||e.startsWith(`http://`)}function r(t,i){if(typeof t==`string`)for(let r of t.matchAll(e)){let e=r[2]?.trim();e&&n(e)&&i.add(e)}else if(Array.isArray(t))for(let e of t)r(e,i)}function i(e){let t=new Set,i=e=>{let a=e=>{e&&(r(e.backgroundImage,t),r(e.maskImage,t))};if(a(e.style),a(e.preset),r(e.tw,t),e.type===`image`){typeof e.src==`string`&&n(e.src)&&t.add(e.src);return}if(e.type===`container`)for(let t of e.children??[])i(t)};return i(e),[...t]}function a(e,n,r){let i=r?.get(e);if(i)return i;let a=t(e,n).then(e=>e.arrayBuffer()).catch(t=>{throw r?.delete(e),t});return r?.set(e,a),a}async function o({node:e,sources:t=[],fetchCache:n,fetch:r,timeout:o=5e3,signal:s,throwOnError:c=!0}){let l=Array.isArray(e)?e:[e],u=new Map;for(let e of t)u.set(e.src,e);let d=[...new Set(l.flatMap(i))].filter(e=>!u.has(e)),f={fetch:r,timeout:o,signal:s},p=d.map(async e=>({src:e,data:await a(e,f,n)})),m=c?await Promise.all(p):(await Promise.allSettled(p)).filter(e=>e.status===`fulfilled`).map(e=>e.value);return[...u.values(),...m]}function s(e,t,n,r){let i=new Map(r),a=n.includes(`italic`),o=a?n.includes(`normal`)?[0,1]:[1]:[void 0],s=[...a?[`ital`]:[],...i.keys(),`wght`].sort(),c=o.flatMap(e=>t.map(t=>s.map(n=>n===`ital`?String(e):n===`wght`?t:i.get(n)??``).join(`,`))).sort();return`${e}:${s.join(`,`)}@${c.join(`;`)}`}function c(e){let t=new URL(e.baseUrl??`https://fonts.googleapis.com/css2`);for(let n of e.families){if(typeof n==`string`){t.searchParams.append(`family`,s(n,[`400`],[`normal`],[]));continue}let e=n.weight??400,r=(Array.isArray(e)?[...e].sort((e,t)=>e-t):[e]).map(String),i=n.style??`normal`,a=(Array.isArray(i)?i:[i]).map(String),o=Object.entries(n.axes??{}).filter(([e])=>e!==`ital`&&e!==`wght`).map(([e,t])=>[e,String(t)]);t.searchParams.append(`family`,s(n.name,r,a,o))}return e.display&&t.searchParams.set(`display`,e.display),t.toString()}function l(e,n){return t(e,{...n,init:{headers:{"User-Agent":`Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36`}}}).then(e=>e.text())}async function u(e,t){let n=t.cache?.get(e);if(n!==void 0)return n;let r=await l(e,t);return t.cache?.set(e,r),r}const d=/(?:\/\*\s*([^*]+?)\s*\*\/\s*)?@font-face\s*\{([^}]*)\}/g;function f(e,n){let r=`${e.family} ${e.subset}`,i=e.ranges.map(([e,t])=>`${e}-${t}`).join(`,`);return{name:r,subsetOf:e.family,key:`${r}:${e.weight??``}:${e.style??``}:${i}`,weight:e.weight,style:e.style,ranges:e.ranges,data:()=>t(e.url,n).then(e=>e.arrayBuffer())}}function p(e){let t=[];for(let n of e.split(`,`)){let e=n.trim().replace(/^U\+/i,``);if(e)if(e.includes(`-`)){let[n,r]=e.split(`-`);n&&r&&t.push([parseInt(n,16),parseInt(r,16)])}else if(e.includes(`?`))t.push([parseInt(e.replace(/\?/g,`0`),16),parseInt(e.replace(/\?/g,`F`),16)]);else{let n=parseInt(e,16);t.push([n,n])}}return t}function m(e){let t=[],n=0;for(let r of e.matchAll(d)){let e=r[2];if(!e)continue;let i=e.match(/src:\s*url\(([^)]+)\)/)?.[1]?.replace(/['"]/g,``).trim(),a=e.match(/font-family:\s*['"]?([^'";]+)['"]?/)?.[1]?.trim();if(!i||!a)continue;let o=e.match(/unicode-range:\s*([^;]+)/)?.[1],s=e.match(/font-weight:\s*(\d+)(?:\s+(\d+))?/);t.push({family:a,subset:r[1]?.trim()||`subset-${n}`,url:i,weight:s&&!s[2]?Number(s[1]):void 0,style:e.match(/font-style:\s*([a-z]+)/i)?.[1],ranges:o?p(o):[]}),n+=1}return t}function h(e){let t=new Map;for(let n of e){let e=t.get(n.url)??new Set;e.add(n.weight),t.set(n.url,e)}let n=new Set,r=[];for(let i of e){if(n.has(i.url))continue;n.add(i.url);let e=(t.get(i.url)?.size??0)>1;r.push(e?{...i,weight:void 0}:i)}return r}function g(e){let t=new Set,n=e=>{for(let n of e)t.add(n.codePointAt(0))},r=e=>{e.type===`text`?n(e.text):e.type===`container`&&e.children?.forEach(r)};return typeof e==`string`?n(e):Array.isArray(e)?e.forEach(r):r(e),t}function _(e,t){if(e.length===0)return!0;for(let n of t)for(let[t,r]of e)if(n>=t&&n<=r)return!0;return!1}function v({fonts:e,source:t}){let n=g(t);return e.filter(e=>_(e.ranges??[],n))}function y(e){return{key:e,data:()=>t(e).then(e=>e.arrayBuffer())}}async function b(e){return e.families.length===0?[]:h(m(await u(c(e),e))).map(t=>f(t,e))}export{t as a,v as i,y as n,o,b as r,g as t};
@@ -0,0 +1 @@
1
+ const e=/url\(\s*(['"]?)(.*?)\1\s*\)/g;async function t(e,t={}){let n=t.fetch??globalThis.fetch,r=t.timeout===void 0?void 0:AbortSignal.timeout(t.timeout),i=[t.signal,t.init?.signal,r].filter(e=>e!==void 0),a=i.length?AbortSignal.any(i):void 0,o=await n(e,{...t.init,signal:a});if(!o.ok)throw Error(`HTTP ${o.status} ${o.statusText} fetching ${e}`);return o}function n(e){return e.startsWith(`https://`)||e.startsWith(`http://`)}function r(t,i){if(typeof t==`string`)for(let r of t.matchAll(e)){let e=r[2]?.trim();e&&n(e)&&i.add(e)}else if(Array.isArray(t))for(let e of t)r(e,i)}function i(e){let t=new Set,i=e=>{let a=e=>{e&&(r(e.backgroundImage,t),r(e.maskImage,t))};if(a(e.style),a(e.preset),r(e.tw,t),e.type===`image`){typeof e.src==`string`&&n(e.src)&&t.add(e.src);return}if(e.type===`container`)for(let t of e.children??[])i(t)};return i(e),[...t]}function a(e,n,r){let i=r?.get(e);if(i)return i;let a=t(e,n).then(e=>e.arrayBuffer()).catch(t=>{throw r?.delete(e),t});return r?.set(e,a),a}async function o({node:e,sources:t=[],fetchCache:n,fetch:r,timeout:o=5e3,signal:s,throwOnError:c=!0}){let l=Array.isArray(e)?e:[e],u=new Map;for(let e of t)u.set(e.src,e);let d=[...new Set(l.flatMap(i))].filter(e=>!u.has(e)),f={fetch:r,timeout:o,signal:s},p=d.map(async e=>({src:e,data:await a(e,f,n)})),m=c?await Promise.all(p):(await Promise.allSettled(p)).filter(e=>e.status===`fulfilled`).map(e=>e.value);return[...u.values(),...m]}function s(e,t,n,r){let i=new Map(r),a=n.includes(`italic`),o=a?n.includes(`normal`)?[0,1]:[1]:[void 0],s=[...a?[`ital`]:[],...i.keys(),`wght`].sort(),c=o.flatMap(e=>t.map(t=>s.map(n=>n===`ital`?String(e):n===`wght`?t:i.get(n)??``).join(`,`))).sort();return`${e}:${s.join(`,`)}@${c.join(`;`)}`}function c(e){let t=new URL(e.baseUrl??`https://fonts.googleapis.com/css2`);for(let n of e.families){if(typeof n==`string`){t.searchParams.append(`family`,s(n,[`400`],[`normal`],[]));continue}let e=n.weight??400,r=(Array.isArray(e)?[...e].sort((e,t)=>e-t):[e]).map(String),i=n.style??`normal`,a=(Array.isArray(i)?i:[i]).map(String),o=Object.entries(n.axes??{}).filter(([e])=>e!==`ital`&&e!==`wght`).map(([e,t])=>[e,String(t)]);t.searchParams.append(`family`,s(n.name,r,a,o))}return e.display&&t.searchParams.set(`display`,e.display),t.toString()}function l(e,n){return t(e,{...n,init:{headers:{"User-Agent":`Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36`}}}).then(e=>e.text())}async function u(e,t){let n=t.cache?.get(e);if(n!==void 0)return n;let r=await l(e,t);return t.cache?.set(e,r),r}const d=/(?:\/\*\s*([^*]+?)\s*\*\/\s*)?@font-face\s*\{([^}]*)\}/g;function f(e,n){let r=`${e.family} ${e.subset}`,i=e.ranges.map(([e,t])=>`${e}-${t}`).join(`,`);return{name:r,subsetOf:e.family,key:`${r}:${e.weight??``}:${e.style??``}:${i}`,weight:e.weight,style:e.style,ranges:e.ranges,data:()=>t(e.url,n).then(e=>e.arrayBuffer())}}function p(e){let t=[];for(let n of e.split(`,`)){let e=n.trim().replace(/^U\+/i,``);if(e)if(e.includes(`-`)){let[n,r]=e.split(`-`);n&&r&&t.push([parseInt(n,16),parseInt(r,16)])}else if(e.includes(`?`))t.push([parseInt(e.replace(/\?/g,`0`),16),parseInt(e.replace(/\?/g,`F`),16)]);else{let n=parseInt(e,16);t.push([n,n])}}return t}function m(e){let t=[],n=0;for(let r of e.matchAll(d)){let e=r[2];if(!e)continue;let i=e.match(/src:\s*url\(([^)]+)\)/)?.[1]?.replace(/['"]/g,``).trim(),a=e.match(/font-family:\s*['"]?([^'";]+)['"]?/)?.[1]?.trim();if(!i||!a)continue;let o=e.match(/unicode-range:\s*([^;]+)/)?.[1],s=e.match(/font-weight:\s*(\d+)(?:\s+(\d+))?/);t.push({family:a,subset:r[1]?.trim()||`subset-${n}`,url:i,weight:s&&!s[2]?Number(s[1]):void 0,style:e.match(/font-style:\s*([a-z]+)/i)?.[1],ranges:o?p(o):[]}),n+=1}return t}function h(e){let t=new Map;for(let n of e){let e=t.get(n.url)??new Set;e.add(n.weight),t.set(n.url,e)}let n=new Set,r=[];for(let i of e){if(n.has(i.url))continue;n.add(i.url);let e=(t.get(i.url)?.size??0)>1;r.push(e?{...i,weight:void 0}:i)}return r}function g(e){let t=new Set,n=e=>{for(let n of e)t.add(n.codePointAt(0))},r=e=>{e.type===`text`?n(e.text):e.type===`container`&&e.children?.forEach(r)};return typeof e==`string`?n(e):Array.isArray(e)?e.forEach(r):r(e),t}function _(e,t){if(e.length===0)return!0;for(let n of t)for(let[t,r]of e)if(n>=t&&n<=r)return!0;return!1}function v({fonts:e,source:t}){let n=g(t);return e.filter(e=>_(e.ranges??[],n))}function y(e){return{key:e,data:()=>t(e).then(e=>e.arrayBuffer())}}async function b(e){return e.families.length===0?[]:h(m(await u(c(e),e))).map(t=>f(t,e))}Object.defineProperty(exports,"a",{enumerable:!0,get:function(){return t}}),Object.defineProperty(exports,"i",{enumerable:!0,get:function(){return v}}),Object.defineProperty(exports,"n",{enumerable:!0,get:function(){return y}}),Object.defineProperty(exports,"o",{enumerable:!0,get:function(){return o}}),Object.defineProperty(exports,"r",{enumerable:!0,get:function(){return b}}),Object.defineProperty(exports,"t",{enumerable:!0,get:function(){return g}});