markdown-magic 3.0.2 → 3.0.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.
Files changed (40) hide show
  1. package/README.md +295 -101
  2. package/cli.js +4 -1
  3. package/lib/block-parser.js +32 -28
  4. package/lib/block-parser.test.js +2 -0
  5. package/lib/cli.js +101 -22
  6. package/lib/cli.test.js +12 -12
  7. package/lib/index.js +418 -119
  8. package/lib/process-contents.js +59 -23
  9. package/lib/process-file.js +39 -4
  10. package/lib/transforms/code.js +33 -10
  11. package/lib/transforms/file.js +4 -2
  12. package/lib/transforms/index.js +114 -0
  13. package/lib/transforms/sectionToc.js +2 -2
  14. package/lib/transforms/toc.js +22 -4
  15. package/lib/transforms/wordCount.js +2 -2
  16. package/lib/utils/fs.js +8 -172
  17. package/lib/utils/fs.test.js +4 -162
  18. package/lib/utils/hash-file.js +28 -0
  19. package/lib/utils/logs.js +16 -2
  20. package/lib/utils/syntax.js +1 -0
  21. package/lib/utils/text.js +1 -1
  22. package/lib/utils/toposort.js +131 -0
  23. package/package.json +4 -3
  24. package/lib/globals.d.ts +0 -66
  25. package/lib/utils/md/filters.js +0 -20
  26. package/lib/utils/md/find-code-blocks.js +0 -88
  27. package/lib/utils/md/find-date.js +0 -32
  28. package/lib/utils/md/find-frontmatter.js +0 -92
  29. package/lib/utils/md/find-frontmatter.test.js +0 -17
  30. package/lib/utils/md/find-html-tags.js +0 -105
  31. package/lib/utils/md/find-images-md.js +0 -27
  32. package/lib/utils/md/find-images.js +0 -107
  33. package/lib/utils/md/find-links.js +0 -220
  34. package/lib/utils/md/find-unmatched-html-tags.js +0 -32
  35. package/lib/utils/md/fixtures/2022-01-22-date-in-filename.md +0 -14
  36. package/lib/utils/md/fixtures/file-with-frontmatter.md +0 -32
  37. package/lib/utils/md/fixtures/file-with-links.md +0 -153
  38. package/lib/utils/md/md.test.js +0 -105
  39. package/lib/utils/md/parse.js +0 -146
  40. package/lib/utils/md/utils.js +0 -19
@@ -1,146 +0,0 @@
1
- const { parse } = require('micro-mdx-parser')
2
- const { parseFrontmatter } = require('./find-frontmatter')
3
- const { findUnmatchedHtmlTags } = require('./find-unmatched-html-tags')
4
- const { findLinks } = require('./find-links')
5
- const { findDate } = require('./find-date')
6
- const { findCodeBlocks, REMOVE_CODE_BLOCK_REGEX } = require('./find-code-blocks')
7
- const { getLineCount } = require('./utils')
8
-
9
- function parseMarkdown(text, opts = {}) {
10
- const {
11
- filePath,
12
- validator,
13
- astParser,
14
- includeAst = true,
15
- includeLinks = true,
16
- includeImages = true,
17
- includeCodeBlocks = true,
18
- includePositions = false,
19
- includeRawFrontmatter = false,
20
- } = opts
21
- let errors = []
22
- let result = {}
23
- let alreadySetError = false
24
- try {
25
- result = parseFrontmatter(text)
26
- } catch (err) {
27
- console.log(`Broken frontmatter in ${filePath}...`)
28
- errors.push(err.message)
29
- alreadySetError = true
30
- }
31
- const { data, content = '', frontMatterRaw = '' } = result
32
- if (!data || !Object.keys(data).length) {
33
- if (!alreadySetError) {
34
- errors.push(`Missing or broken frontmatter in ${filePath}. Double check file for --- frontmatter tags`)
35
- }
36
- }
37
-
38
- let links
39
- let images
40
- if (includeLinks || includeImages) {
41
- const linkData = findLinks(text, {
42
- frontmatter: data
43
- })
44
- links = linkData.links
45
- images = linkData.images
46
- }
47
-
48
- let ast = []
49
- if (includeAst) {
50
- /* If custom parser supplied */
51
- if (astParser) {
52
- ast = astParser(content, opts)
53
- } else {
54
- /* Default parser */
55
- ast = parse(content, {
56
- includePositions,
57
- // offset: {
58
- // lineOffset: getLineCount(frontMatterRaw),
59
- // charOffset: frontMatterRaw.length
60
- // }
61
- })
62
- }
63
- }
64
-
65
- // console.log('html', html)
66
- // console.log(`htmlTags ${filePath}`, htmlTags)
67
- let codeBlocks
68
- if (includeCodeBlocks) {
69
- codeBlocks = findCodeBlocks(text, { filePath, includePositions })
70
- }
71
-
72
- // console.log(`codeBlocks ${filePath}`, codeBlocks)
73
- const tagsErrors = findUnmatchedHtmlTags(text, filePath)
74
-
75
- // const htmlValidationTags = validateHtmlTags(htmlTags, filePath)
76
- // if (htmlValidationTags && htmlValidationTags.length) {
77
- // errors = errors.concat(htmlValidationTags)
78
- // }
79
-
80
- let htmlValidation = []
81
- if (typeof validator === 'function') {
82
- // const contentsNoCodeBlocks = content.replace(REMOVE_CODE_BLOCK_REGEX, '')
83
- htmlValidation = validator(content, filePath)
84
- }
85
-
86
- if (htmlValidation && htmlValidation.length) {
87
- // console.log('htmlValidation', htmlValidation)
88
- errors = errors.concat(htmlValidation)
89
- }
90
-
91
- if (tagsErrors && tagsErrors.length) {
92
- errors = errors.concat(tagsErrors)
93
- }
94
-
95
- if (codeBlocks.errors && codeBlocks.errors.length) {
96
- errors = errors.concat(codeBlocks.errors)
97
- }
98
-
99
- const frontmatter = data || {}
100
- const date = findDate({
101
- frontmatter,
102
- filePath
103
- })
104
-
105
- const parseResult = {}
106
-
107
- if (filePath) {
108
- parseResult.filePath = filePath
109
- }
110
-
111
- if (date) {
112
- parseResult.date = date
113
- }
114
-
115
- if (includeAst) {
116
- parseResult.ast = ast
117
- }
118
-
119
- /* Include frontmatter as data object */
120
- parseResult.data = frontmatter
121
-
122
- if (includeRawFrontmatter) {
123
- parseResult.frontMatterRaw = frontMatterRaw
124
- }
125
-
126
- if (includeLinks) {
127
- parseResult.links = links
128
- }
129
-
130
- if (includeImages) {
131
- parseResult.images = images
132
- }
133
-
134
- if (includeCodeBlocks) {
135
- parseResult.codeBlocks = codeBlocks.blocks
136
- }
137
-
138
- parseResult.content = content
139
- parseResult.errors = errors
140
-
141
- return parseResult
142
- }
143
-
144
- module.exports = {
145
- parseMarkdown
146
- }
@@ -1,19 +0,0 @@
1
-
2
-
3
- function getLineNumberFromMatch(text = '', matches) {
4
- return getLineCount(text.substr(0, matches.index))
5
- }
6
-
7
- function getLines(str = '') {
8
- return str.split(/\r\n|\r|\n/)
9
- }
10
-
11
- function getLineCount(str = '') {
12
- return getLines(str).length
13
- }
14
-
15
- module.exports = {
16
- getLines,
17
- getLineCount,
18
- getLineNumberFromMatch
19
- }