-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Wojciech Krysiak
authored and
Wojciech Krysiak
committed
Sep 20, 2020
1 parent
81a2bee
commit 9ac8622
Showing
6 changed files
with
82 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { expect } from 'chai' | ||
import { fillComponentPreview } from './fill-component-preview' | ||
|
||
|
||
const text = [ | ||
'My Patterns.', | ||
'', | ||
'Explore results with the Tools below. Replace & List output custom results. Details lists capture groups. Explain describes your expression in plain English.', | ||
'', | ||
'```reactComponent', | ||
'', | ||
'const a = 1;', | ||
'return (', | ||
' <div>ala</div>', | ||
')', | ||
'', | ||
'```', | ||
'asdasdas', | ||
'da', | ||
'sda', | ||
'```reactComponent', | ||
'', | ||
'const a = 2;', | ||
'return (', | ||
' <div>ala</div>', | ||
')', | ||
'', | ||
'```', | ||
'sdasd', | ||
'asd', | ||
].join('\n') | ||
|
||
|
||
describe.only('.fillComponentPreview', () => { | ||
it('chanes ```reactComponent ``` to react code', () => { | ||
expect(fillComponentPreview(text)).to.contain('sth') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const regex = /^```reactComponent[\r\n]+(.*?)[\r\n]+```$/gms | ||
|
||
export const fillComponentPreview = (text: string, mdParser): string => { | ||
let number = 0 | ||
|
||
const components = {} | ||
|
||
const result = text.replace(regex, (match, group) => { | ||
const uniqId = ['BetterDocsPreviewComponent', number += 1].join('') | ||
|
||
components[uniqId] = ` | ||
<div id="${uniqId}"></div> | ||
<script> | ||
ReactDOM.render(ReactWrapper({ | ||
example: ${JSON.stringify(group)}, | ||
uniqId: ${JSON.stringify(uniqId)}, | ||
}), document.getElementById('${uniqId}')); | ||
</script> | ||
` | ||
|
||
return uniqId | ||
}) | ||
|
||
let markdown = mdParser(result) | ||
|
||
Object.keys(components).forEach((componentId) => { | ||
markdown = markdown.replace(componentId, components[componentId]) | ||
}) | ||
|
||
return markdown | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters