tellegram 1.1.2 → 1.1.4

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);
@@ -12,7 +12,7 @@ const trim = (str, opts) => ensureString(str, { trim: true, ...opts || {} });
12
12
  // Is
13
13
  const _is = (type, value) => value?.constructor === type;
14
14
  const _type = (any) => typeof any === 'undefined' ? 'Undefined'
15
- : Object.prototype.toString.call(any).replace(/^\[[^\ ]*\ (.*)\]$/, '$1');
15
+ : Object.prototype.toString.call(any).replace(/^\[[^ ]* (.*)\]$/, '$1');
16
16
  [
17
17
  ArrayBuffer, BigInt, Boolean, Error, Number, Object, Set, String, Uint8Array
18
18
  ].map(type => {
@@ -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,23 @@ 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
+ // cut = Math.min(cut, size);
92
+ // while (countLength(cut) > size) { cut--; }
93
+ if (countLength(cut) > size) {
94
+ let high = Math.min(cut, size);
95
+ cut = 0;
96
+ while (cut < high) {
97
+ let mid = Math.ceil((cut + high) / 2);
98
+ if (countLength(mid) <= size) {
99
+ cut = mid;
100
+ } else {
101
+ high = mid - 1;
102
+ }
92
103
  }
93
- cut--;
94
- concat = '...';
95
104
  }
96
- if (shouldBreak) { continue; }
105
+ concat = cut < nextN ? '...' : '';
97
106
  const line = message.substring(0, cut + 1).trimEnd();
98
107
  page.push(line);
99
108
  /^```.{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
@@ -136,7 +136,7 @@ const createHandlers = (definitions, unsupportedTagsStrategy) => ({
136
136
  processUnsupportedTags(defaultHandlers.html(node, _parent, context), unsupportedTagsStrategy),
137
137
  table: (node, _parent, context) =>
138
138
  processUnsupportedTags(gfmTableToMarkdown().handlers.table(node, _parent, context), unsupportedTagsStrategy),
139
- thematicBreak: (_node, _parent, _context) =>
139
+ thematicBreak: () =>
140
140
  processUnsupportedTags('---', unsupportedTagsStrategy),
141
141
  });
142
142
 
@@ -31,7 +31,7 @@ export function escapeSymbols(text, textType = 'text') {
31
31
  .replace(/\(/g, '\\(')
32
32
  .replace(/\)/g, '\\)')
33
33
  case 'ignore_escaped':
34
- return text.replace(/(\\.)|([_*\[\]()~`>#+\-=|{}.!])/g, (match, escaped, char) => {
34
+ return text.replace(/(\\.)|([_*[\]()~`>#+\-=|{}.!])/g, (match, escaped, char) => {
35
35
  if (escaped) {
36
36
  return escaped;
37
37
  }
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "tellegram",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
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
- "lint": "eslint",
9
+ "lint": "eslint . --ext .js,.mjs",
10
10
  "semantic-release": "semantic-release",
11
11
  "prepare": "husky install",
12
12
  "codecov": "codecov"
@@ -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
  ],
@@ -61,11 +61,11 @@
61
61
  "semantic-release": "^17.4.2"
62
62
  },
63
63
  "lint-staged": {
64
- "*.{js,json,md}": [
64
+ "*.{js,mjs,json,md}": [
65
65
  "prettier --write",
66
66
  "git add"
67
67
  ],
68
68
  "*.{css,scss,less}": "stylelint --fix",
69
- "*.js": "eslint --cache --fix"
69
+ "*.{js,mjs}": "eslint --cache --fix"
70
70
  }
71
71
  }
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