@prunellejs/images 0.7.3 → 0.7.6

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/card.ts +59 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prunellejs/images",
3
- "version": "0.7.3",
3
+ "version": "0.7.6",
4
4
  "description": "Build-time responsive AVIF/WebP image optimization plugin for Prunelle",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -22,6 +22,6 @@
22
22
  "directory": "packages/plugin-images"
23
23
  },
24
24
  "peerDependencies": {
25
- "prunelle": "^0.7.3"
25
+ "prunelle": "^0.7.6"
26
26
  }
27
27
  }
package/src/card.ts CHANGED
@@ -69,24 +69,73 @@ function getCardConfig(): ShareCardDefaults {
69
69
  }
70
70
 
71
71
 
72
- function wrapLines(text: string, maxChars: number, maxLines: number): string[] {
72
+ function splitTokens(text: string, maxChars: number): string[] {
73
73
  const words = text.trim().split(/\s+/).filter(Boolean)
74
+ const tokens: string[] = []
75
+ for (const w of words) {
76
+ const chars = Array.from(w)
77
+ if (chars.length <= maxChars) {
78
+ tokens.push(w)
79
+ }
80
+ else {
81
+ for (let i = 0; i < chars.length; i += maxChars)
82
+ tokens.push(chars.slice(i, i + maxChars).join(''))
83
+ }
84
+ }
85
+ return tokens
86
+ }
87
+
88
+ function addEllipsis(line: string, maxChars: number): string {
89
+ const chars = Array.from(line)
90
+ if (line.endsWith('…') && chars.length <= maxChars)
91
+ return line
92
+ if (chars.length < maxChars)
93
+ return `${line}…`
94
+ if (maxChars <= 1)
95
+ return '…'
96
+ const lastSpace = line.lastIndexOf(' ')
97
+ if (lastSpace > 0 && Array.from(line.slice(0, lastSpace)).length < maxChars)
98
+ return `${line.slice(0, lastSpace).trimEnd()}…`
99
+ return `${chars.slice(0, maxChars - 1).join('').trimEnd()}…`
100
+ }
101
+
102
+ function wrapLines(text: string, maxChars: number, maxLines: number): string[] {
103
+ if (maxChars < 1 || maxLines < 1)
104
+ return []
105
+ const tokens = splitTokens(text, maxChars)
106
+ if (tokens.length === 0)
107
+ return []
108
+
74
109
  const lines: string[] = []
75
110
  let cur = ''
76
- for (const w of words) {
77
- const next = cur ? `${cur} ${w}` : w
78
- if (next.length > maxChars && cur) {
111
+ let curLen = 0
112
+ let truncated = false
113
+
114
+ for (let i = 0; i < tokens.length; i++) {
115
+ const token = tokens[i]!
116
+ const tokenLen = Array.from(token).length
117
+ if (curLen === 0) {
118
+ cur = token
119
+ curLen = tokenLen
120
+ }
121
+ else if (curLen + 1 + tokenLen <= maxChars) {
122
+ cur = `${cur} ${token}`
123
+ curLen += 1 + tokenLen
124
+ }
125
+ else if (lines.length < maxLines - 1) {
79
126
  lines.push(cur)
80
- cur = w
81
- if (lines.length >= maxLines - 1)
82
- break
127
+ cur = token
128
+ curLen = tokenLen
83
129
  }
84
130
  else {
85
- cur = next
131
+ truncated = true
132
+ break
86
133
  }
87
134
  }
88
- if (cur && lines.length < maxLines)
89
- lines.push(cur)
135
+
136
+ if (cur)
137
+ lines.push(truncated ? addEllipsis(cur, maxChars) : cur)
138
+
90
139
  return lines
91
140
  }
92
141