-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from hougesen/feat/parse-code-blocks
feat: parse code blocks
- Loading branch information
Showing
8 changed files
with
114 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { expect, it, describe } from 'vitest'; | ||
import { generateCodeBlock } from '../src/utils/generateCodeBlock'; | ||
|
||
describe('generateCodeBlock.ts', () => { | ||
it('valid code block ', () => { | ||
const languages = ['', 'js', 'ts', 'java', 'rust']; | ||
|
||
for (const language of languages) { | ||
expect(generateCodeBlock(['```' + language, 'const x = 10;', 'const y = Math.pow(x,x);', '```'])).toEqual( | ||
`<code${ | ||
language ? `lang="${language}" class="codeblock codeblock--${language}"` : '' | ||
}>const x = 10; <br /> const y = Math.pow(x,x);</code>` | ||
); | ||
} | ||
}); | ||
}); |
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,21 @@ | ||
This is before the code block | ||
|
||
|
||
|
||
|
||
|
||
```js | ||
const x = 10; | ||
const y = x * x; | ||
``` | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
This is after the code block |
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
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,17 @@ | ||
export function generateCodeBlock(code: string[], attributes = ''): string { | ||
const language = code?.shift()?.replace('```', '')?.trim() ?? ''; | ||
|
||
if (language?.length) { | ||
if (attributes?.includes('class="')) { | ||
attributes.replace('class="', `class="codeblock codeblock--${language}`); | ||
} else { | ||
attributes += ` class="codeblock codeblock--${language}"`; | ||
} | ||
} | ||
|
||
code.pop(); | ||
|
||
return `<code${language ? `lang="${language}"` : ''}${attributes}>${code | ||
?.filter((l) => l?.trim()?.length && l?.includes('```') === false) | ||
?.join(' <br /> ')}</code>`; | ||
} |
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