-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support to import files as code fence (#538)
- Loading branch information
Showing
9 changed files
with
112 additions
and
0 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,43 @@ | ||
const fs = require('fs') | ||
|
||
module.exports = function snippet (md, options = {}) { | ||
const root = options.root || process.cwd() | ||
function parser (state, startLine, endLine, silent) { | ||
const CH = '<'.charCodeAt(0) | ||
const pos = state.bMarks[startLine] + state.tShift[startLine] | ||
const max = state.eMarks[startLine] | ||
|
||
// if it's indented more than 3 spaces, it should be a code block | ||
if (state.sCount[startLine] - state.blkIndent >= 4) { | ||
return false | ||
} | ||
|
||
for (let i = 0; i < 3; ++i) { | ||
const ch = state.src.charCodeAt(pos + i) | ||
if (ch !== CH || pos + i >= max) return false | ||
} | ||
|
||
if (silent) { | ||
return true | ||
} | ||
|
||
const start = pos + 3 | ||
const end = state.skipSpacesBack(max, pos) | ||
const rawPath = state.src.slice(start, end).trim().replace(/^@/, root) | ||
const filename = rawPath.split(/[{:\s]/).shift() | ||
const content = fs.existsSync(filename) ? fs.readFileSync(filename).toString() : 'Not found: ' + filename | ||
const meta = rawPath.replace(filename, '') | ||
|
||
state.line = startLine + 1 | ||
|
||
const token = state.push('fence', 'code', 0) | ||
token.info = filename.split('.').pop() + meta | ||
token.content = content | ||
token.markup = '```' | ||
token.map = [startLine, startLine + 1] | ||
|
||
return true | ||
} | ||
|
||
md.block.ruler.before('fence', 'snippet', parser) | ||
} |
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,23 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`snippet import snippet 1`] = ` | ||
<pre><code class="language-js">export default function () { | ||
// .. | ||
}</code></pre> | ||
`; | ||
|
||
exports[`snippet import snippet with highlight multiple lines 1`] = ` | ||
<div class="highlight-lines"> | ||
<div class="highlighted"> </div> | ||
<div class="highlighted"> </div> | ||
<div class="highlighted"> </div> | ||
</div>export default function () { // .. } | ||
`; | ||
exports[`snippet import snippet with highlight single line 1`] = ` | ||
<div class="highlight-lines"> | ||
<div class="highlighted"> </div> | ||
<br> | ||
<div class="highlighted"> </div> | ||
</div>export default function () { // .. } | ||
`; |
1 change: 1 addition & 0 deletions
1
test/markdown/fragments/code-snippet-highlightLines-multiple.md
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 @@ | ||
<<< @/test/markdown/fragments/snippet.js{1-3} |
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 @@ | ||
<<< @/test/markdown/fragments/snippet.js{1,3} |
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 @@ | ||
<<< @/test/markdown/fragments/snippet.js |
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,3 @@ | ||
export default function () { | ||
// .. | ||
} |
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,26 @@ | ||
import { Md, getFragment } from './util' | ||
import snippet from '@/markdown/snippet.js' | ||
import highlightLines from '@/markdown/highlightLines.js' | ||
|
||
const md = Md().use(snippet) | ||
const mdH = Md().use(snippet).use(highlightLines) | ||
|
||
describe('snippet', () => { | ||
test('import snippet', async () => { | ||
const input = await getFragment('code-snippet') | ||
const output = md.render(input) | ||
expect(output).toMatchSnapshot() | ||
}) | ||
|
||
test('import snippet with highlight single line', async () => { | ||
const input = await getFragment('code-snippet-highlightLines-single') | ||
const output = mdH.render(input) | ||
expect(output).toMatchSnapshot() | ||
}) | ||
|
||
test('import snippet with highlight multiple lines', async () => { | ||
const input = await getFragment('code-snippet-highlightLines-multiple') | ||
const output = mdH.render(input) | ||
expect(output).toMatchSnapshot() | ||
}) | ||
}) |