-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add extensions support #38
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6605148
feat: add extensions support
genericFJS 3b9ebb7
chore: Add test for extension
genericFJS b0532eb
chore: document component option for extension
genericFJS c4e21b7
fix: optimize extension test tokenizer regex
genericFJS 3c6861e
fix: improve typing of use parameter
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export { Lexer, Slugger } from 'marked' | ||
export { Lexer, Slugger, marked } from 'marked' | ||
|
||
import { | ||
Heading, | ||
|
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,6 @@ | ||
<script> | ||
export let value = 0 | ||
export let message = '' | ||
</script> | ||
|
||
<span>{message}: {value}</span> |
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,44 @@ | ||
import '@testing-library/jest-dom/extend-expect' | ||
|
||
import { render, screen } from '@testing-library/svelte' | ||
|
||
import SvelteMarkdown from '../src/SvelteMarkdown.svelte' | ||
import TestRenderer from './TestRenderer.svelte' | ||
|
||
describe('testing extensions', () => { | ||
beforeAll(() => { | ||
console.warn = jest.fn() | ||
}) | ||
|
||
test('renders a custom token with an extension and custom component', () => { | ||
const testTokenizerExtension = { | ||
name: 'test', | ||
level: 'inline', | ||
start(src) { | ||
return src.match(/\[\[/).index | ||
}, | ||
tokenizer(src) { | ||
const rule = /^\[\[test(?:(?=.*?\svalue="(.*?)".*?|.*)(?=.*?\smessage="(.*?)(?<!\\)".*?|.*).*?|\s*)\]\]/ | ||
const match = rule.exec(src) | ||
if (match) { | ||
return { | ||
type: 'test', | ||
raw: match[0], | ||
value: match[1], | ||
message: match[2], | ||
} | ||
} | ||
}, | ||
} | ||
|
||
render(SvelteMarkdown, { | ||
source: '[[test value="5" message="test message"]]', | ||
use: { extensions: [testTokenizerExtension] }, | ||
renderers: { test: TestRenderer }, | ||
}) | ||
|
||
const element = screen.getByText('test message', { exact: false }) | ||
expect(element).toBeInTheDocument() | ||
expect(element).toContainHTML('<span>test message: 5</span>') | ||
}) | ||
}) |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're using all of marked now, is there a difference on build size between importing
Lexer
vs doingnew marked.Lexer()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did some rudimentary tests with the svelte template where I added a SvelteMarkdown component with an extension (as in the first comment of this PR):
There were no differences in files size (after
yarn build
) when using it as is VS removing the Lexer/Slugger imports entirely and writingand
Removing the imports seems to have no effect on build size (and could just be done for cosmetic reasons).