tellegram 1.1.2 → 1.1.3

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/index.mjs ADDED
@@ -0,0 +1,5 @@
1
+ import convert from './lib/convert.mjs';
2
+ import paginate from './lib/paginate.mjs';
3
+
4
+ export { convert, paginate };
5
+ export default convert;
@@ -4,8 +4,8 @@ import stringify from 'remark-stringify';
4
4
  import removeComments from 'remark-remove-comments';
5
5
  import unified from 'unified';
6
6
 
7
- import { collectDefinitions, removeDefinitions } from './definitions.js';
8
- import createTellegramOptions from './tellegram.js';
7
+ import { collectDefinitions, removeDefinitions } from './definitions.mjs';
8
+ import createTellegramOptions from './tellegram.mjs';
9
9
 
10
10
  export default (markdown, unsupportedTagsStrategy = 'escape') => {
11
11
  const definitions = {};
@@ -1,5 +1,5 @@
1
1
  import assert from 'assert';
2
- import convert from './convert.js';
2
+ import convert from './convert.mjs';
3
3
 
4
4
  const MESSAGE_LENGTH_LIMIT = parseInt(4096 * 0.93); // ~= 3800
5
5
  const clarify = str => str.toLowerCase().split(/[^a-zA-Z0-9]+/).filter(x => x);
@@ -66,7 +66,10 @@ const ensureString = (str, options) => {
66
66
  export default (message, options) => {
67
67
  let [pages, page, size, codeMark, concat, prefix] =
68
68
  [[], [], ~~options?.size || MESSAGE_LENGTH_LIMIT, '', '', ''];
69
- const countLength = str => convert(str).length;
69
+ const countLength = pos => {
70
+ const str = prefix + lines(page) + message.substring(0, pos + 1);
71
+ return Math.max(str.length, convert(str).length);
72
+ };
70
73
  const submit = () => {
71
74
  const content = trim(lines(page));
72
75
  content && pages.push(prefix + content + concat + (codeMark ? '\n```' : ''));
@@ -83,17 +86,19 @@ export default (message, options) => {
83
86
  while ((message || '').length) {
84
87
  let nextN = message.indexOf('\n'); // 獲得下一個換行
85
88
  nextN === -1 && (nextN = message.length); // 剩下只有一行
86
- let [cut, shouldBreak] = [nextN, false]; // 初始化當前預測裁切
87
- while (countLength(lines(page) + message.substring(0, cut + 1)) > size) {
88
- if (page.length) {
89
- submit();
90
- shouldBreak = true;
91
- break;
89
+ let cut = nextN; // 初始化當前預測裁切
90
+ if (countLength(cut) > size && page.length) { submit(); continue; }
91
+ let high = Math.min(cut, size);
92
+ cut = 0;
93
+ while (cut < high) {
94
+ let mid = Math.ceil((cut + high) / 2);
95
+ if (countLength(mid) <= size) {
96
+ cut = mid;
97
+ } else {
98
+ high = mid - 1;
92
99
  }
93
- cut--;
94
- concat = '...';
95
100
  }
96
- if (shouldBreak) { continue; }
101
+ concat = cut < nextN ? '...' : '';
97
102
  const line = message.substring(0, cut + 1).trimEnd();
98
103
  page.push(line);
99
104
  /^```.{0,20}$/.test(line) && (codeMark = codeMark ? '' : line);
@@ -2,7 +2,7 @@ import defaultHandlers from 'mdast-util-to-markdown/lib/handle/index.js';
2
2
  import phrasing from 'mdast-util-to-markdown/lib/util/container-phrasing.js';
3
3
  import { toMarkdown as gfmTableToMarkdown } from 'mdast-util-gfm-table';
4
4
 
5
- import { wrap, isURL, escapeSymbols, processUnsupportedTags } from './utils.js';
5
+ import { wrap, isURL, escapeSymbols, processUnsupportedTags } from './utils.mjs';
6
6
 
7
7
  /**
8
8
  * Creates custom `mdast-util-to-markdown` handlers that tailor the output for
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "tellegram",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "Convert LLM-generated markdown into Telegram-specific markdown (MarkdownV2)",
5
5
  "type": "module",
6
- "main": "index.js",
6
+ "main": "index.mjs",
7
7
  "scripts": {
8
8
  "test": "NODE_OPTIONS='--experimental-vm-modules --no-deprecation' npx jest --coverage --verbose",
9
9
  "lint": "eslint",
@@ -14,7 +14,7 @@
14
14
  "files": [
15
15
  "README.md",
16
16
  "LICENSE",
17
- "index.js",
17
+ "index.mjs",
18
18
  "lib",
19
19
  "types"
20
20
  ],
package/index.js DELETED
@@ -1,5 +0,0 @@
1
- import convert from './lib/convert.js';
2
- import paginate from './lib/paginate.js';
3
-
4
- export { convert, paginate };
5
- export default convert;
File without changes
File without changes