markdown-magic 3.3.2 → 3.4.0
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/lib/utils/toc.js +9 -5
- package/package.json +3 -3
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/lib/utils/toc.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const {
|
|
1
|
+
const { generateToc : generateTocUtils } = require('@davidwells/md-utils')
|
|
2
2
|
const { removeLeadingAndTrailingLineBreaks, escapeRegexString } = require('./regex')
|
|
3
3
|
const { findMinIndent } = require('./text')
|
|
4
4
|
const { readFile } = require('./fs')
|
|
@@ -75,8 +75,11 @@ async function generateToc({
|
|
|
75
75
|
// separator: '<span>|</span>',
|
|
76
76
|
// footer: 'end',
|
|
77
77
|
}
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
|
|
79
|
+
const tocObject = generateTocUtils(contents)
|
|
80
|
+
|
|
81
|
+
// let outputText = t.wrappedToc || ''
|
|
82
|
+
let outputText = tocObject.text || ''
|
|
80
83
|
|
|
81
84
|
if (debugFileMatch) {
|
|
82
85
|
console.log('before firsth1 removal', outputText)
|
|
@@ -247,7 +250,9 @@ async function generateToc({
|
|
|
247
250
|
// console.log('single', single)
|
|
248
251
|
const subItems = outputText.match(findSubToc)
|
|
249
252
|
if (subItems) {
|
|
250
|
-
const items = subItems[0]
|
|
253
|
+
const items = subItems[0]
|
|
254
|
+
.replace(single, '')
|
|
255
|
+
.split('\n')
|
|
251
256
|
.filter(Boolean)
|
|
252
257
|
// console.log('items', items)
|
|
253
258
|
const finalItems = items // .slice(1, items.length)
|
|
@@ -277,7 +282,6 @@ ${outputText
|
|
|
277
282
|
|
|
278
283
|
</details>`
|
|
279
284
|
}
|
|
280
|
-
|
|
281
285
|
return outputText.replace(removeLeadingAndTrailingLineBreaks, '')
|
|
282
286
|
}
|
|
283
287
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "markdown-magic",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.4.0",
|
|
4
4
|
"description": "Automatically update markdown files with content from external sources",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -43,8 +43,7 @@
|
|
|
43
43
|
"url": "https://github.com/DavidWells/markdown-magic"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@davidwells/md-utils": "^0.0.
|
|
47
|
-
"@technote-space/doctoc": "2.4.7",
|
|
46
|
+
"@davidwells/md-utils": "^0.0.44",
|
|
48
47
|
"globrex": "^0.1.2",
|
|
49
48
|
"gray-matter": "^4.0.3",
|
|
50
49
|
"is-glob": "^4.0.3",
|
|
@@ -55,6 +54,7 @@
|
|
|
55
54
|
"node-fetch": "^2.7.0",
|
|
56
55
|
"oparser": "^3.0.22",
|
|
57
56
|
"smart-glob": "^1.0.2",
|
|
57
|
+
"string-width": "^4.2.3",
|
|
58
58
|
"sync-request": "^6.1.0"
|
|
59
59
|
},
|
|
60
60
|
"devDependencies": {
|