markdown-magic 3.3.2 → 3.3.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/lib/index.js +7 -1
- package/lib/utils/string-break.js +140 -0
- package/package.json +2 -1
package/lib/index.js
CHANGED
|
@@ -9,6 +9,7 @@ const remoteTransform = require('./transforms/remote')
|
|
|
9
9
|
const { getSyntaxInfo } = require('./utils/syntax')
|
|
10
10
|
const { onlyUnique, getCodeLocation, pluralize } = require('./utils')
|
|
11
11
|
const { readFile, resolveOutputPath, resolveFlatPath } = require('./utils/fs')
|
|
12
|
+
const stringBreak = require('./utils/string-break')
|
|
12
13
|
const { processFile } = require('./process-file')
|
|
13
14
|
const { processContents } = require('./process-contents')
|
|
14
15
|
const { parseMarkdown } = require('@davidwells/md-utils')
|
|
@@ -737,9 +738,14 @@ async function asyncForEach(array, callback) {
|
|
|
737
738
|
}
|
|
738
739
|
}
|
|
739
740
|
|
|
741
|
+
const stringUtils = {
|
|
742
|
+
stringBreak
|
|
743
|
+
}
|
|
744
|
+
|
|
740
745
|
module.exports = {
|
|
741
746
|
markdownMagic,
|
|
742
747
|
parseMarkdown,
|
|
743
748
|
processContents,
|
|
744
|
-
processFile
|
|
749
|
+
processFile,
|
|
750
|
+
stringUtils
|
|
745
751
|
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
const stringWidth = require('string-width')
|
|
2
|
+
|
|
3
|
+
const openPunctuations = [
|
|
4
|
+
'“',
|
|
5
|
+
'‘',
|
|
6
|
+
'(',
|
|
7
|
+
'《',
|
|
8
|
+
'〈',
|
|
9
|
+
'〔',
|
|
10
|
+
'【',
|
|
11
|
+
|
|
12
|
+
'(',
|
|
13
|
+
'[',
|
|
14
|
+
'{',
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
function isOpenPunctuation(char) {
|
|
18
|
+
return openPunctuations.includes(char)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const closePunctuations = [
|
|
22
|
+
'。',
|
|
23
|
+
'?',
|
|
24
|
+
'!',
|
|
25
|
+
',',
|
|
26
|
+
'、',
|
|
27
|
+
';',
|
|
28
|
+
'”',
|
|
29
|
+
'’',
|
|
30
|
+
')',
|
|
31
|
+
'》',
|
|
32
|
+
'〉',
|
|
33
|
+
'〕',
|
|
34
|
+
'】',
|
|
35
|
+
|
|
36
|
+
'.',
|
|
37
|
+
'?',
|
|
38
|
+
'!',
|
|
39
|
+
',',
|
|
40
|
+
';',
|
|
41
|
+
')',
|
|
42
|
+
']',
|
|
43
|
+
'}',
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
function isClosePunctuation(char) {
|
|
47
|
+
return closePunctuations.includes(char)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const otherPunctuations = [
|
|
51
|
+
':',
|
|
52
|
+
'─',
|
|
53
|
+
'…',
|
|
54
|
+
'·',
|
|
55
|
+
|
|
56
|
+
':',
|
|
57
|
+
'-',
|
|
58
|
+
'–',
|
|
59
|
+
'—',
|
|
60
|
+
'"',
|
|
61
|
+
"'",
|
|
62
|
+
]
|
|
63
|
+
|
|
64
|
+
function isPunctuation(char) {
|
|
65
|
+
return (
|
|
66
|
+
openPunctuations.includes(char) ||
|
|
67
|
+
closePunctuations.includes(char) ||
|
|
68
|
+
otherPunctuations.includes(char)
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function isFullWidthChar(char) {
|
|
73
|
+
return /[\u4e00-\u9fa5]/.test(char)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function getNextBreakPoint(str, width, from, lastIndex) {
|
|
77
|
+
let idealIndex = from
|
|
78
|
+
const length = str.length
|
|
79
|
+
let subWidth = 0
|
|
80
|
+
|
|
81
|
+
do {
|
|
82
|
+
idealIndex++
|
|
83
|
+
subWidth = stringWidth(str.slice(from, idealIndex))
|
|
84
|
+
} while (subWidth <= width && idealIndex <= length)
|
|
85
|
+
|
|
86
|
+
let index = idealIndex - 1
|
|
87
|
+
|
|
88
|
+
if (index >= length) {
|
|
89
|
+
return length
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
while (index > lastIndex) {
|
|
93
|
+
const preValue = str.charAt(index - 1)
|
|
94
|
+
const value = str.charAt(index)
|
|
95
|
+
const canBreak =
|
|
96
|
+
preValue === ' ' ||
|
|
97
|
+
value === ' ' ||
|
|
98
|
+
(isFullWidthChar(value) && isFullWidthChar(preValue)) ||
|
|
99
|
+
(isClosePunctuation(preValue) && !isPunctuation(value)) ||
|
|
100
|
+
(!isPunctuation(preValue) && isOpenPunctuation(value)) ||
|
|
101
|
+
(isClosePunctuation(preValue) && isOpenPunctuation(value))
|
|
102
|
+
|
|
103
|
+
if (canBreak) {
|
|
104
|
+
break
|
|
105
|
+
} else {
|
|
106
|
+
index--
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (index <= lastIndex) {
|
|
111
|
+
index = idealIndex
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return index
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function stringBreak(str, width) {
|
|
118
|
+
if (width < 2) {
|
|
119
|
+
throw new Error('Width must be greater than 2')
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const length = str.length
|
|
123
|
+
let index = 0
|
|
124
|
+
let breakPoint = 0
|
|
125
|
+
let line
|
|
126
|
+
const lines = []
|
|
127
|
+
|
|
128
|
+
while (index < length) {
|
|
129
|
+
breakPoint = getNextBreakPoint(str, width, index, breakPoint)
|
|
130
|
+
line = str.slice(index, breakPoint).trim()
|
|
131
|
+
|
|
132
|
+
lines.push(line)
|
|
133
|
+
|
|
134
|
+
index = breakPoint
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return lines
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
module.exports = stringBreak
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "markdown-magic",
|
|
3
|
-
"version": "3.3.
|
|
3
|
+
"version": "3.3.3",
|
|
4
4
|
"description": "Automatically update markdown files with content from external sources",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -55,6 +55,7 @@
|
|
|
55
55
|
"node-fetch": "^2.7.0",
|
|
56
56
|
"oparser": "^3.0.22",
|
|
57
57
|
"smart-glob": "^1.0.2",
|
|
58
|
+
"string-width": "^4.2.3",
|
|
58
59
|
"sync-request": "^6.1.0"
|
|
59
60
|
},
|
|
60
61
|
"devDependencies": {
|