markdown-magic 2.6.1 → 3.0.1
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/README.md +47 -37
- package/cli.js +5 -82
- package/lib/block-parser-js.test.js +171 -0
- package/lib/block-parser.js +382 -0
- package/lib/block-parser.test.js +479 -0
- package/lib/cli.js +245 -0
- package/lib/cli.test.js +409 -0
- package/lib/defaults.js +12 -0
- package/lib/globals.d.ts +66 -0
- package/lib/index.js +353 -184
- package/lib/index.test.js +11 -0
- package/lib/process-contents.js +371 -0
- package/lib/process-file.js +37 -0
- package/lib/transforms/code.js +67 -28
- package/lib/transforms/file.js +17 -17
- package/lib/transforms/index.js +0 -114
- package/lib/transforms/remote.js +8 -6
- package/lib/transforms/sectionToc.js +18 -0
- package/lib/transforms/toc.js +12 -265
- package/lib/transforms/wordCount.js +5 -0
- package/lib/types.js +11 -0
- package/lib/utils/fs.js +342 -0
- package/lib/utils/fs.test.js +267 -0
- package/lib/utils/index.js +19 -0
- package/{cli-utils.js → lib/utils/load-config.js} +2 -6
- package/lib/utils/logs.js +94 -0
- package/lib/utils/md/filters.js +20 -0
- package/lib/utils/md/find-code-blocks.js +88 -0
- package/lib/utils/md/find-date.js +32 -0
- package/lib/utils/md/find-frontmatter.js +92 -0
- package/lib/utils/md/find-frontmatter.test.js +17 -0
- package/lib/utils/md/find-html-tags.js +105 -0
- package/lib/utils/md/find-images-md.js +27 -0
- package/lib/utils/md/find-images.js +107 -0
- package/lib/utils/md/find-links.js +220 -0
- package/lib/utils/md/find-unmatched-html-tags.js +32 -0
- package/lib/utils/md/fixtures/2022-01-22-date-in-filename.md +14 -0
- package/lib/utils/md/fixtures/file-with-frontmatter.md +32 -0
- package/lib/utils/md/fixtures/file-with-links.md +153 -0
- package/lib/utils/md/md.test.js +105 -0
- package/lib/utils/md/parse.js +146 -0
- package/lib/utils/md/utils.js +19 -0
- package/lib/utils/regex-timeout.js +84 -0
- package/lib/utils/regex.js +40 -6
- package/lib/utils/remoteRequest.js +55 -0
- package/lib/utils/syntax.js +82 -0
- package/lib/utils/text.js +328 -0
- package/lib/utils/text.test.js +305 -0
- package/lib/utils/toc.js +315 -0
- package/package.json +30 -26
- package/index.js +0 -46
- package/lib/processFile.js +0 -154
- package/lib/updateContents.js +0 -125
- package/lib/utils/_md.test.js +0 -63
- package/lib/utils/new-parser.js +0 -412
- package/lib/utils/new-parser.test.js +0 -324
- package/lib/utils/weird-parse.js +0 -230
- package/lib/utils/weird-parse.test.js +0 -217
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Welcome to ABC
|
|
3
|
+
description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
|
|
4
|
+
date: '2020-06-30'
|
|
5
|
+
thumbnail: https://dope-frontmatter.com/img/deploy/button.svg
|
|
6
|
+
components:
|
|
7
|
+
- type: PageHeading
|
|
8
|
+
heading: Nice
|
|
9
|
+
subHeading: Add it
|
|
10
|
+
- type: content
|
|
11
|
+
content: >-
|
|
12
|
+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur
|
|
13
|
+
|
|
14
|
+
<a href="https://funky-frontmatter.com">funky</a>
|
|
15
|
+
|
|
16
|
+
<img src='/img/in-nested-frontmatter/button.svg' />
|
|
17
|
+
|
|
18
|
+

|
|
19
|
+
|
|
20
|
+
<img src='https://frontmatter.com/img/deploy/button.svg' />
|
|
21
|
+
|
|
22
|
+
we've created a [detailed blog post](https://www.front.com/blog/open-beta-changes) to help those with the migration process. We're confident these changes, once released, will make for a significantly better experience for current and future users.
|
|
23
|
+
categories:
|
|
24
|
+
- company
|
|
25
|
+
authors:
|
|
26
|
+
- Bob Bob
|
|
27
|
+
- Joe Smoe
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
**Welcome to ABC!** Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur [keynote presentation](https://youtu.be/A1bL4pHuivU).
|
|
31
|
+
|
|
32
|
+
<img src='/img/deploy/button.svg' />
|
|
33
|
+
|
|
34
|
+
<img src='./img/deploy/button.svg' />
|
|
35
|
+
|
|
36
|
+
<img src='../img/deploy/button.svg' />
|
|
37
|
+
|
|
38
|
+
<img src="https://www.netlify.com/img/deploy/button.svg" />
|
|
39
|
+
|
|
40
|
+
<img src="https://www.netlify.com/img/deploy/button.svg" />
|
|
41
|
+
|
|
42
|
+
<img src='https://fooo.com/img/deploy/button.svg' />
|
|
43
|
+
|
|
44
|
+

|
|
45
|
+
|
|
46
|
+

|
|
47
|
+
|
|
48
|
+

|
|
49
|
+
|
|
50
|
+

|
|
51
|
+
|
|
52
|
+
This is the weird markdown link syntax <https://foooooooooooo.com>
|
|
53
|
+
|
|
54
|
+
this is less than sign
|
|
55
|
+
|
|
56
|
+
Hear Bob Bobster discuss the evolution of Serverless and the launch of ABC at the AWS Serverless Community Day.
|
|
57
|
+
|
|
58
|
+
<iframe width="560" height="315" src="https://www.youtube.com/embed/KX7tj3giizI" frameBorder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowFullScreen></iframe>
|
|
59
|
+
|
|
60
|
+
<a href="https://app.netlify.com/start/deploy">
|
|
61
|
+
<img src="/img/deploy/button.svg">
|
|
62
|
+
</a>
|
|
63
|
+
|
|
64
|
+
<a href="/foobar">
|
|
65
|
+
<img src="/img/deploy/button.svg">
|
|
66
|
+
</a>
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
<a href="https://www.yoursite.com/pricing?utm_source=active%20users&utm_medium=email&utm_campaign=feature%20launch&utm_content=bottom%20cta%20button">
|
|
70
|
+
Utm params link
|
|
71
|
+
</a>
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
Title xyz
|
|
75
|
+
============================
|
|
76
|
+
|
|
77
|
+
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem
|
|
78
|
+
|
|
79
|
+
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem
|
|
80
|
+
|
|
81
|
+
Legacy Alternatives
|
|
82
|
+
===================
|
|
83
|
+
|
|
84
|
+
It vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id
|
|
85
|
+
|
|
86
|
+
Escaping the "Single Box" Blockchain Limitation
|
|
87
|
+
===============================================
|
|
88
|
+
|
|
89
|
+
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
|
|
90
|
+
|
|
91
|
+
<Author foo='bar' foo='bing'>
|
|
92
|
+
<Child />
|
|
93
|
+
</Author>
|
|
94
|
+
|
|
95
|
+
```graphql
|
|
96
|
+
mutation _files__add($input: ABC_File_Input_!, $syncMode: ABC_SyncMode = NODE_COMMITTED)
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
To override the default, include the `syncMode` option as such:
|
|
100
|
+
|
|
101
|
+
```js
|
|
102
|
+
/* Hello there */
|
|
103
|
+
const response = await entities.product.add(
|
|
104
|
+
{
|
|
105
|
+
name: 'super-widget',
|
|
106
|
+
inventory: 100,
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
syncMode: 'ASYNC', <-- example syncMode instruction
|
|
110
|
+
}
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
console.log(response?.transaction?.transactionId);
|
|
114
|
+
console.log(response?.transaction?._id);
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Title xyz
|
|
118
|
+
===========================
|
|
119
|
+
|
|
120
|
+
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat
|
|
121
|
+
|
|
122
|
+
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat
|
|
123
|
+
|
|
124
|
+
<!-- doc-gen {{functionName}} foo={{ rad: 'yellow' }} -->
|
|
125
|
+
xyz
|
|
126
|
+
<!-- end-doc-gen -->
|
|
127
|
+
|
|
128
|
+
Hello title
|
|
129
|
+
=======================
|
|
130
|
+
|
|
131
|
+
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat remaining confident that their solutions will work on _any_ public cloud, not just a single vendor. Interested in experiencing it for yourself? [Sign up](https://ABC.com/sign-up) to join our preview access program, starting in late July.
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
<!-- doc-gen {{functionName}}
|
|
135
|
+
foo={{ rad: 'yellow' }}
|
|
136
|
+
baz=bar
|
|
137
|
+
fun=['array']
|
|
138
|
+
-->
|
|
139
|
+
xyz
|
|
140
|
+
<!-- end-doc-gen -->
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
We’re excited to be on this journey to help customers solve some of their greatest challenges by making code and data easier to share, protect, and secure. **Welcome to ABC and welcome to a new era of sharing made easy!**
|
|
144
|
+
|
|
145
|
+
<Author
|
|
146
|
+
arr={['one', 'two']}
|
|
147
|
+
foo={{ "bar": true }}
|
|
148
|
+
baz='string'
|
|
149
|
+
/>
|
|
150
|
+
|
|
151
|
+
_Bob & Joe_
|
|
152
|
+
|
|
153
|
+
_Looking for your next great opportunity?_ [_We’re hiring!_](http://jobs.ABC.net)
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
const fs = require('fs')
|
|
2
|
+
const path = require('path')
|
|
3
|
+
const { test } = require('uvu')
|
|
4
|
+
const assert = require('uvu/assert')
|
|
5
|
+
const { deepLog } = require('../logs')
|
|
6
|
+
const { parseMarkdown } = require('./parse')
|
|
7
|
+
const { findLinks } = require('./find-links')
|
|
8
|
+
const { parseFrontmatter } = require('./find-frontmatter')
|
|
9
|
+
|
|
10
|
+
const FILE_PATH = path.join(__dirname, 'fixtures/file-with-links.md')
|
|
11
|
+
// const FILE_PATH = path.join(__dirname, 'fixtures/2022-01-22-date-in-filename.md')
|
|
12
|
+
const fileContents = fs.readFileSync(FILE_PATH, 'utf-8')
|
|
13
|
+
|
|
14
|
+
test('parseMarkdown API', async () => {
|
|
15
|
+
const res = parseMarkdown(fileContents, { filePath: FILE_PATH })
|
|
16
|
+
assert.is(typeof res, 'object')
|
|
17
|
+
assert.is(typeof res.ast, 'object')
|
|
18
|
+
assert.is(typeof res.data, 'object')
|
|
19
|
+
assert.is(typeof res.content, 'string')
|
|
20
|
+
assert.is(typeof res.codeBlocks, 'object')
|
|
21
|
+
assert.is(Array.isArray(res.errors), true)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
test('Verify contents', async () => {
|
|
25
|
+
const res = parseMarkdown(fileContents, {
|
|
26
|
+
filePath: FILE_PATH
|
|
27
|
+
})
|
|
28
|
+
deepLog('Results:', res)
|
|
29
|
+
assert.equal(res.links, [
|
|
30
|
+
'https://funky-frontmatter.com',
|
|
31
|
+
'https://www.front.com/blog/open-beta-changes',
|
|
32
|
+
'https://youtu.be/A1bL4pHuivU',
|
|
33
|
+
'https://foooooooooooo.com',
|
|
34
|
+
'https://www.youtube.com/embed/KX7tj3giizI',
|
|
35
|
+
'https://app.netlify.com/start/deploy',
|
|
36
|
+
'https://www.yoursite.com/pricing?utm_source=active%20users&utm_medium=email&utm_campaign=feature%20launch&utm_content=bottom%20cta%20button',
|
|
37
|
+
'https://ABC.com/sign-up',
|
|
38
|
+
'http://jobs.ABC.net',
|
|
39
|
+
'/foobar'
|
|
40
|
+
])
|
|
41
|
+
assert.equal(res.images, [
|
|
42
|
+
'/assets/images/lol-frontmatter.jpg',
|
|
43
|
+
'/assets/images/lol.jpg',
|
|
44
|
+
'assets/images/san-juan-mountains.jpg',
|
|
45
|
+
'https://res.cloudinary.com/ABC/image/upload/f_auto,q_auto/c_fill,w_1200/v1668114635/what-you-can-build_p8uape.png',
|
|
46
|
+
'https://avatars2.githubusercontent.com/u/532272?v=3&s=400',
|
|
47
|
+
'https://dope-frontmatter.com/img/deploy/button.svg',
|
|
48
|
+
'https://frontmatter.com/img/deploy/button.svg',
|
|
49
|
+
'/img/in-nested-frontmatter/button.svg',
|
|
50
|
+
'https://www.netlify.com/img/deploy/button.svg',
|
|
51
|
+
'https://fooo.com/img/deploy/button.svg',
|
|
52
|
+
'/img/deploy/button.svg',
|
|
53
|
+
'img/deploy/button.svg',
|
|
54
|
+
'../img/deploy/button.svg'
|
|
55
|
+
])
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
test('opts - includeAst false', async () => {
|
|
59
|
+
const res = parseMarkdown(fileContents, {
|
|
60
|
+
filePath: FILE_PATH,
|
|
61
|
+
})
|
|
62
|
+
// deepLog('Results:', res)
|
|
63
|
+
assert.ok(Array.isArray(res.ast))
|
|
64
|
+
/* Disable AST */
|
|
65
|
+
const resTwo = parseMarkdown(fileContents, {
|
|
66
|
+
filePath: FILE_PATH,
|
|
67
|
+
includeAst: false,
|
|
68
|
+
})
|
|
69
|
+
// deepLog('Results two:', resTwo)
|
|
70
|
+
assert.ok(typeof resTwo.ast === 'undefined')
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
test('opts - includeImages false', async () => {
|
|
74
|
+
const res = parseMarkdown(fileContents, {
|
|
75
|
+
filePath: FILE_PATH,
|
|
76
|
+
// includeAst: false,
|
|
77
|
+
includeImages: false,
|
|
78
|
+
})
|
|
79
|
+
deepLog('Results:', res)
|
|
80
|
+
assert.ok(typeof res.links === 'object')
|
|
81
|
+
assert.ok(typeof res.images === 'undefined')
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
test('opts - includeLinks false', async () => {
|
|
85
|
+
const res = parseMarkdown(fileContents, {
|
|
86
|
+
filePath: FILE_PATH,
|
|
87
|
+
includeLinks: false,
|
|
88
|
+
})
|
|
89
|
+
deepLog('Results:', res)
|
|
90
|
+
assert.ok(typeof res.links === 'undefined')
|
|
91
|
+
assert.ok(typeof res.images === 'object')
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
// test('File have correct extensions', async () => {
|
|
95
|
+
// const { data } = parseFrontmatter(fileWithLinks, FILE_PATH)
|
|
96
|
+
// console.log('frontmatter data', data)
|
|
97
|
+
// const links = findLinks(fileWithLinks, { frontmatter: data })
|
|
98
|
+
// // console.log('links', links)
|
|
99
|
+
// // console.log('FILE_PATH', FILE_PATH)
|
|
100
|
+
// // console.log('parseMarkdown')
|
|
101
|
+
// // const x = parseMarkdown(fileWithLinks, { filePath: FILE_PATH })
|
|
102
|
+
// // deepLog(x)
|
|
103
|
+
// })
|
|
104
|
+
|
|
105
|
+
test.run()
|
|
@@ -0,0 +1,146 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// https://stackoverflow.com/questions/38859506/cancel-regex-match-if-timeout
|
|
2
|
+
const util = require('util')
|
|
3
|
+
const vm = require('vm')
|
|
4
|
+
const { getBlockRegex } = require('../block-parser')
|
|
5
|
+
const { getSyntaxInfo } = require('./syntax')
|
|
6
|
+
|
|
7
|
+
const goodString = `
|
|
8
|
+
# Test Fixture
|
|
9
|
+
|
|
10
|
+
This is normal text in markdown. <!-- doc-start (wordCount) -->x<!-- doc-end -->
|
|
11
|
+
Keep it.
|
|
12
|
+
|
|
13
|
+
<!-- doc-start (wordCount) -->
|
|
14
|
+
THIS CONTENT GETS AUTO GENERATED. Don't directly edit it
|
|
15
|
+
<!-- doc-end -->
|
|
16
|
+
|
|
17
|
+
This is normal text in markdown. Keep it.
|
|
18
|
+
`
|
|
19
|
+
|
|
20
|
+
const badString = `
|
|
21
|
+
# Test Fixture
|
|
22
|
+
|
|
23
|
+
This is normal text in markdown. <!-- doc-start (wordCount) -->x<!-- oc-end -->
|
|
24
|
+
Keep it.
|
|
25
|
+
|
|
26
|
+
<!-- doc-start (wordCount) -->
|
|
27
|
+
THIS CONTENT GETS AUTO GENERATED. Don't directly edit it
|
|
28
|
+
<!-- xoc-end -->
|
|
29
|
+
|
|
30
|
+
This is normal text in markdown. Keep it.
|
|
31
|
+
`
|
|
32
|
+
|
|
33
|
+
// var pattern = /([ \t]*)(?:<!-{2,}(?:.*|\r?|\n?|\s*)docs-start\s*([(\[\{]*[A-Za-z0-9_$-]*[)\]\}]*)\s*)((?:.*?|.*?\r?\n?)*?)<!-{2,}(?:.*|\r?|\n?|\s*)docs-end(?:.|\r?\n)*?-{2,}>/gim
|
|
34
|
+
|
|
35
|
+
function safeRegex(str) {
|
|
36
|
+
const syntax = 'md'
|
|
37
|
+
const syntaxInfo = getSyntaxInfo(syntax)
|
|
38
|
+
if (!syntaxInfo.pattern) {
|
|
39
|
+
throw new Error(`Unknown syntax "${syntax}"`)
|
|
40
|
+
}
|
|
41
|
+
const [ openComment, closeComment ] = syntaxInfo.pattern
|
|
42
|
+
const pattern = getBlockRegex({
|
|
43
|
+
openComment,
|
|
44
|
+
closeComment,
|
|
45
|
+
openText: 'doc-start',
|
|
46
|
+
closeText: 'doc-end'
|
|
47
|
+
})
|
|
48
|
+
console.log('Pattern', pattern)
|
|
49
|
+
const sandbox = {
|
|
50
|
+
// regex: /([ \t]*)(?:<!-{2,}(?:.*|\r?|\n?|\s*)docs-start\s*([(\[\{]*[A-Za-z0-9_$-]*[)\]\}]*)\s*)((?:.*?|.*?\r?\n?)*?)<!-{2,}(?:.*|\r?|\n?|\s*)docs-end(?:.|\r?\n)*?-{2,}>/gim,
|
|
51
|
+
regexToUse: pattern,
|
|
52
|
+
string: str,
|
|
53
|
+
//string: badStringTwo,
|
|
54
|
+
//string: string,
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const context = vm.createContext(sandbox)
|
|
58
|
+
console.log('Sandbox initialized: ' + vm.isContext(sandbox))
|
|
59
|
+
// var script = new vm.Script('result = regex.exec(string)');
|
|
60
|
+
const script = new vm.Script(`
|
|
61
|
+
blocks = []
|
|
62
|
+
rawMatches = string.match(regexToUse)
|
|
63
|
+
while ((list = regexToUse.exec(string)) !== null) {
|
|
64
|
+
const [ block, spaces, __type, params ] = list
|
|
65
|
+
blocks.push({
|
|
66
|
+
__type,
|
|
67
|
+
block
|
|
68
|
+
})
|
|
69
|
+
if (list.index === regexToUse.lastIndex) regexToUse.lastIndex++
|
|
70
|
+
}
|
|
71
|
+
`);
|
|
72
|
+
try {
|
|
73
|
+
// One could argue if a RegExp hasn't processed in a given time.
|
|
74
|
+
// then, its likely it will take exponential time.
|
|
75
|
+
script.runInContext(context, { timeout: 1000 }); // milliseconds
|
|
76
|
+
} catch (e) {
|
|
77
|
+
console.log('ReDos occurred', e); // Take some remedial action here...
|
|
78
|
+
}
|
|
79
|
+
console.log('result:')
|
|
80
|
+
console.log(util.inspect(sandbox)); // Check the results
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// safeRegex(goodString)
|
|
84
|
+
safeRegex(badString)
|
package/lib/utils/regex.js
CHANGED
|
@@ -1,19 +1,35 @@
|
|
|
1
1
|
|
|
2
|
-
|
|
2
|
+
// REGEX to look for regex
|
|
3
|
+
// https://github.com/kgryte/regex-regex/blob/master/lib/index.js
|
|
4
|
+
const REGEX_REGEX = /^\/((?:\\\/|[^\/])+)\/([imgy]*)$/
|
|
5
|
+
const LINEBREAK_MATCHER = /\r\n|[\r\n\u2028\u2029]/;
|
|
6
|
+
|
|
7
|
+
function escapeRegexString(string) {
|
|
8
|
+
if (typeof string !== 'string') {
|
|
9
|
+
throw new TypeError('Expected a string')
|
|
10
|
+
}
|
|
11
|
+
// Escape characters with special meaning either inside or outside character sets.
|
|
12
|
+
// Use a simple backslash escape when it’s always valid, and a `\xnn` escape when the simpler form would be disallowed by Unicode patterns’ stricter grammar.
|
|
13
|
+
return string
|
|
14
|
+
.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
|
|
15
|
+
.replace(/-/g, '\\x2d');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function matchCommentBlock(word) {
|
|
3
19
|
//return new RegExp(`(?:\\<\\!--(?:.|\\r?\\n)*?${matchWord}:START(?:.|\\r?\\n)*?\\()(.*)\\)(?:.|\\r?\\n)*?<!--(?:.|\\r?\\n)*?${matchWord}:END(?:.|\\r?\\n)*?--\\>`, 'g')
|
|
4
20
|
return new RegExp(`(.*)(?:\\<\\!--(?:.*|\r?|\n?|\s*)${word}:START(?:.|\\r?\\n)*?\\()(.*)\\)(?:.|\\r?\\n)*?<!--(?:.*|\r?|\n?|\s*)${word}:END(?:.|\\r?\\n)*?--\\>`, 'g')
|
|
5
21
|
}
|
|
6
22
|
|
|
7
|
-
|
|
23
|
+
function matchOpeningCommentTag(word) {
|
|
8
24
|
// return new RegExp(`(\\<\\!--(?:.|\\r?\\n)*?${matchWord}:START)((?:.|\\r?\\n)*?--\\>)`, 'g')
|
|
9
25
|
return new RegExp(`(\\<\\!--(?:.*|\r?|\n?|\s*)${word}:START)((?:.|\\r?\\n)*?--\\>)`, 'g')
|
|
10
26
|
}
|
|
11
27
|
|
|
12
|
-
|
|
28
|
+
function matchClosingCommentTag(word) {
|
|
13
29
|
return new RegExp(`--\\>(?:.|\\r?\\n)*?((?:\\<\\!--(?:.*|\\r?\\n)(?:.*|\\r?\\n))*?${word}:END)((?:.|\\r?\\n)*?--\\>)`, 'g')
|
|
14
30
|
}
|
|
15
31
|
|
|
16
|
-
|
|
32
|
+
const removeLeadingAndTrailingLineBreaks = /^(?:[\t ]*(?:\r?\n|\r))+|\s+$/g
|
|
17
33
|
|
|
18
34
|
const jsRegex = /\/\* Step([\s\S]*?)\*\//g
|
|
19
35
|
const ymlRegex = / *?# Step([\s\S]*?) #\n*?/g
|
|
@@ -39,7 +55,7 @@ function cleanStepMatches(matches) {
|
|
|
39
55
|
/**
|
|
40
56
|
* Match comment steps in files
|
|
41
57
|
*/
|
|
42
|
-
function getSteps() {
|
|
58
|
+
function getSteps(matches) {
|
|
43
59
|
const steps = cleanStepMatches(matches)
|
|
44
60
|
|
|
45
61
|
const sortedSteps = steps.reduce((accumulator, currentValue, currentIndex, array) => {
|
|
@@ -56,6 +72,15 @@ function getSteps() {
|
|
|
56
72
|
})
|
|
57
73
|
}
|
|
58
74
|
|
|
75
|
+
/*
|
|
76
|
+
Alt attribute regex https://regex101.com/r/Ughcb7/1
|
|
77
|
+
const attrNameRegex = "[a-z0-9_]+";
|
|
78
|
+
const attrValueRegex = '(\\\\"|[^"])*';
|
|
79
|
+
const attrRegex = `(${attrNameRegex})\\s*=\\s*"(${attrValueRegex})"`;
|
|
80
|
+
const keyValue = new RegExp(attrRegex, "gi");
|
|
81
|
+
*/
|
|
82
|
+
|
|
83
|
+
|
|
59
84
|
// Regex to remove all comment blocks
|
|
60
85
|
const REMOVE_COMMENTS_REGEX = / *?\<\!-- ([\s\S]*?) ?--\>\n\n*?/g
|
|
61
86
|
// content.replace(removeComments, '')
|
|
@@ -73,4 +98,13 @@ const TABEL_ROW_REGEX = /^\s*\|.*?\|\s*$/
|
|
|
73
98
|
* @param {string} text The text to validate.
|
|
74
99
|
* @returns {boolean}
|
|
75
100
|
*/
|
|
76
|
-
const isTableRow = (text) => text.match(TABEL_ROW_REGEX)
|
|
101
|
+
const isTableRow = (text) => Boolean(text.match(TABEL_ROW_REGEX))
|
|
102
|
+
|
|
103
|
+
module.exports = {
|
|
104
|
+
REGEX_REGEX,
|
|
105
|
+
escapeRegexString,
|
|
106
|
+
matchCommentBlock,
|
|
107
|
+
matchOpeningCommentTag,
|
|
108
|
+
matchClosingCommentTag,
|
|
109
|
+
removeLeadingAndTrailingLineBreaks,
|
|
110
|
+
}
|
|
@@ -3,6 +3,7 @@ const request = require('sync-request')
|
|
|
3
3
|
module.exports = function remoteRequest(url) {
|
|
4
4
|
let body
|
|
5
5
|
try {
|
|
6
|
+
// @ts-expect-error
|
|
6
7
|
const res = request('GET', url)
|
|
7
8
|
body = res.getBody('utf8')
|
|
8
9
|
} catch (e) {
|
|
@@ -11,3 +12,57 @@ module.exports = function remoteRequest(url) {
|
|
|
11
12
|
}
|
|
12
13
|
return body
|
|
13
14
|
}
|
|
15
|
+
|
|
16
|
+
/*
|
|
17
|
+
TODO add file caching?
|
|
18
|
+
*/
|
|
19
|
+
// const path = require('path')
|
|
20
|
+
// const cacheManager = require('cache-manager')
|
|
21
|
+
// const fsStoreHash = require('cache-manager-fs-hash')
|
|
22
|
+
// const CACHE_KEY = 'foo'
|
|
23
|
+
// const STORAGE_PATH = (process.env.IS_OFFLINE) ? path.join(__dirname, '../tmp') : '/tmp'
|
|
24
|
+
// const SECONDS = 60
|
|
25
|
+
// const MINUTES = 60
|
|
26
|
+
// const ONE_HOUR = SECONDS * MINUTES
|
|
27
|
+
// const mbOfStorage = 512
|
|
28
|
+
// /* initialize caching on disk */
|
|
29
|
+
// const diskCache = cacheManager.caching({
|
|
30
|
+
// store: fsStoreHash,
|
|
31
|
+
// options: {
|
|
32
|
+
// /* TTL in seconds */
|
|
33
|
+
// ttl: ONE_HOUR,
|
|
34
|
+
// /* max size in bytes on disk */
|
|
35
|
+
// // maxsize: mbOfStorage * 1000 * 1000,
|
|
36
|
+
// path: STORAGE_PATH,
|
|
37
|
+
// }
|
|
38
|
+
// })
|
|
39
|
+
|
|
40
|
+
// async function usage() {
|
|
41
|
+
// const hasCache = await diskCache.get(CACHE_KEY)
|
|
42
|
+
// if (hasCache && hasCache.length) {
|
|
43
|
+
// /* If cache NOT empty return it */
|
|
44
|
+
// // console.log('Using cached value', hasCache)
|
|
45
|
+
// return hasCache
|
|
46
|
+
// }
|
|
47
|
+
// // else do fetch
|
|
48
|
+
// const data = await getStuff()
|
|
49
|
+
// // Then save cache
|
|
50
|
+
// console.log('Saving value')
|
|
51
|
+
// await diskCache.set(CACHE_KEY, data)
|
|
52
|
+
// }
|
|
53
|
+
|
|
54
|
+
// function getCacheSize(filePath) {
|
|
55
|
+
// return new Promise((resolve, reject) => {
|
|
56
|
+
// fs.stat(filePath, (err, stats) => {
|
|
57
|
+
// if (err) {
|
|
58
|
+
// return resolve({ sizeInBytes: 0, sizeInMB: 0 })
|
|
59
|
+
// }
|
|
60
|
+
// const byteSize = stats.size
|
|
61
|
+
// const megaByteSize = byteSize / (1024 * 1024)
|
|
62
|
+
// return resolve({
|
|
63
|
+
// sizeInBytes: byteSize,
|
|
64
|
+
// sizeInMB: megaByteSize
|
|
65
|
+
// })
|
|
66
|
+
// })
|
|
67
|
+
// })
|
|
68
|
+
// }
|