Skip to content
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
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ The SvelteMarkdown component accepts the following options:
- `source` - _string_ The Markdown source to be parsed.
- `renderers` - _object (optional)_ An object where the keys represent a node type and the value is a Svelte component. This object will be merged with the default renderers. For now you can check how the default renderers are written in the source code at `src/renderers`.
- `options` - _object (optional)_ An object containing [options for Marked](https://marked.js.org/using_advanced#options)
- `use` - _object (optional)_ An object containing [extensions for Marked]([marked.use](https://marked.js.org/using_pro#use))

## Events

Expand Down
8 changes: 6 additions & 2 deletions src/SvelteMarkdown.svelte
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<script>
import { setContext, createEventDispatcher, onMount } from 'svelte'
import Parser from './Parser.svelte'
import { Lexer, Slugger, defaultOptions, defaultRenderers } from './markdown-parser'
import { Lexer, Slugger, defaultOptions, defaultRenderers, marked } from './markdown-parser'
import { key } from './context'

export let source = ''
export let renderers = {}
export let options = {}
export let isInline = false
export let use = {}

const dispatch = createEventDispatcher();

Expand All @@ -18,7 +19,10 @@
$: slugger = source ? new Slugger : undefined
$: combinedOptions = { ...defaultOptions, ...options }
$: {
lexer = new Lexer(combinedOptions)
marked.setOptions(combinedOptions)
marked.use(use)

lexer = new Lexer()
Copy link
Owner

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 doing new marked.Lexer()?

Copy link
Author

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 writing

slugger = source ? new marked.Slugger : undefined

and

tokens = isInline ? marked.Lexer.lexInline(source) : marked.Lexer.lex(source)

Removing the imports seems to have no effect on build size (and could just be done for cosmetic reasons).


tokens = isInline ? lexer.inlineTokens(source) : lexer.lex(source)

Expand Down
2 changes: 1 addition & 1 deletion src/markdown-parser.js
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,
Expand Down
6 changes: 6 additions & 0 deletions tests/TestRenderer.svelte
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>
44 changes: 44 additions & 0 deletions tests/extensions.spec.js
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>')
})
})
6 changes: 6 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {
marked,
MarkedExtension as MarkedConfig,
Tokens,
TokensList,
Expand Down Expand Up @@ -71,6 +72,11 @@ type Props = {
*/
renderers?: Partial<Renderers>

/**
* Parameters for the function [marked.use](https://marked.js.org/using_pro#use)
*/
use?: Omit<marked.MarkedExtension, 'renderer'>[]

/**
* Options for [marked](https://marked.js.org/using_advanced#options)
*/
Expand Down