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

Make elements.code and elements.codespan options work as documented #47

Closed
wants to merge 3 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
63 changes: 42 additions & 21 deletions src/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
exports[`should allow injecting context to components 1`] = `
<div>
<div>
<div>
bar
</div>
bar
</div>
</div>
`;
Expand All @@ -18,7 +16,38 @@ exports[`should allow injecting context to elements 1`] = `
</div>
`;

exports[`should allow overriding code element with components version 1`] = `
exports[`should allow overriding block code element 1`] = `
<div>
<div>
js
:
code
</div>
</div>
`;

exports[`should allow overriding block code element with components version 1`] = `
<div>
<div>
js
:
code
</div>
</div>
`;

exports[`should allow overriding inline code element 1`] = `
<div>
<p>
Hello
<div>
code
</div>
</p>
</div>
`;

exports[`should allow overriding inline code element with components version 1`] = `
<div>
<p>
Hello
Expand Down Expand Up @@ -61,19 +90,15 @@ exports[`should be able to compile codespans 1`] = `
exports[`should be able to compile components 1`] = `
<div>
<div>
<div>
mip
</div>
mip
</div>
</div>
`;

exports[`should be able to compile components using marksy language 1`] = `
<div>
<div>
<div>
mip
</div>
mip
</div>
</div>
`;
Expand Down Expand Up @@ -118,9 +143,7 @@ exports[`should be able to compile html 1`] = `
exports[`should be able to compile html as components 1`] = `
<div>
<div>
<div>
hello
</div>
hello
</div>
</div>
`;
Expand Down Expand Up @@ -326,14 +349,12 @@ exports[`should be able to compile text 1`] = `

exports[`should be able to inline components 1`] = `
<div>
<div>
<p>
Hello there
<div>
Wuuut
</div>
</p>
</div>
<p>
Hello there
<div>
Wuuut
</div>
</p>
</div>
`;

Expand Down
17 changes: 2 additions & 15 deletions src/components.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import createRenderer from './createRenderer';
import createRenderer, {codeRenderer} from './createRenderer';
import marked from 'marked';
import {transform} from 'babel-standalone';

export function marksy (options = {}) {
options.components = options.components || {};

function CodeComponent (props) {
return options.createElement('pre', null, options.createElement('code', {
className: `language-${props.language}`,
dangerouslySetInnerHTML: {__html: options.highlight ? options.highlight(props.language, props.code) : props.code}
}))
}

const tracker = {
tree: null,
elements: null,
Expand Down Expand Up @@ -41,13 +34,7 @@ export function marksy (options = {}) {
if (language === 'marksy') {
return renderer.html(code)
} else {
const elementId = tracker.nextElementId++;

tracker.elements[elementId] = options.createElement((options.elements && options.elements.code) || CodeComponent, {key: elementId, code, language});

tracker.tree.push(tracker.elements[elementId]);

return `{{${elementId}}}`;
return codeRenderer(tracker, options)(code, language);
}
}
})
Expand Down
49 changes: 29 additions & 20 deletions src/createRenderer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
import marked from 'marked';
import he from 'he';

export function codeRenderer(tracker, options) {
function CodeComponent (props) {
return options.createElement('pre', null, options.createElement('code', {
className: `language-${props.language}`,
dangerouslySetInnerHTML: {__html: options.highlight ? options.highlight(props.language, props.code) : props.code}
}))
}

return function(code, language) {
const elementId = tracker.nextElementId++;

tracker.elements[elementId] = options.createElement(
(options.elements && options.elements.code) || CodeComponent,
{key: elementId, code, language}
);

tracker.tree.push(tracker.elements[elementId]);

return `{{${elementId}}}`;
}
}

export default function createRenderer (tracker, options, overrides = {}) {
const renderer = new marked.Renderer();

Expand Down Expand Up @@ -32,39 +54,26 @@ export default function createRenderer (tracker, options, overrides = {}) {
return extractedElements;
}

function addElement (tag, props = {}, children) {
function addElement (tag, props = {}, children, type = tag) {
const elementId = tracker.nextElementId++;
let inlineContent = null;

const elementType = options.elements && (options.elements[type] || options.elements[tag])

if (children) {
inlineContent = Array.isArray(children) ? children.map(populateInlineContent) : populateInlineContent(children)
}

tracker.elements[elementId] = options.createElement((options.elements && options.elements[tag]) || tag, Object.assign({
tracker.elements[elementId] = options.createElement(elementType || tag, Object.assign({
key: elementId,
}, props, options.elements && options.elements[tag] ? {context: tracker.context} : {}), inlineContent);
}, props, elementType ? {context: tracker.context} : {}), inlineContent);

tracker.tree.push(tracker.elements[elementId]);

return `{{${elementId}}}`;
}

renderer.code = overrides.code || function (code, language) {
const elementId = tracker.nextElementId++;

function CodeComponent () {
return options.createElement('pre', null, options.createElement('code', {
className: `language-${language}`,
dangerouslySetInnerHTML: {__html: options.highlight ? options.highlight(language, code) : code}
}))
}

tracker.elements[elementId] = options.createElement(CodeComponent, {key: elementId});

tracker.tree.push(tracker.elements[elementId]);

return `{{${elementId}}}`;
};
renderer.code = overrides.code || codeRenderer(tracker, options);

renderer.html = overrides.html || function (html) {
const elementId = tracker.nextElementId++;
Expand Down Expand Up @@ -172,7 +181,7 @@ export default function createRenderer (tracker, options, overrides = {}) {
}

renderer.codespan = overrides.codespan || function (text) {
return addElement('code', null, text)
return addElement('code', null, text, 'codespan')
}

renderer.image = overrides.image || function (href, title, text) {
Expand Down
61 changes: 58 additions & 3 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,30 @@ it('should be able to inline components', () => {
expect(tree).toMatchSnapshot();
});

it('should allow overriding code element with components version', () => {
it('should allow overriding inline code element', () => {
const compile = marksy({
createElement,
elements: {
codespan({children}) {
return <div>{children}</div>
}
}
});
const compiled = compile('Hello `code`');

const tree = renderer.create(
<TestComponent>{compiled.tree}</TestComponent>
).toJSON();

expect(tree).toMatchSnapshot();
});

it('should allow overriding inline code element with components version', () => {
const compile = marksyComponents({
createElement,
elements: {
code() {
return <div>code</div>
codespan({children}) {
return <div>{children}</div>
}
}
});
Expand All @@ -398,6 +416,43 @@ it('should allow overriding code element with components version', () => {
expect(tree).toMatchSnapshot();
});

it('should allow overriding block code element', () => {
const compile = marksy({
createElement,
elements: {
code({language, code}) {
console.log(language, code)
return <div>{language}: {code}</div>
}
}
});
const compiled = compile('```js\ncode\n```');

const tree = renderer.create(
<TestComponent>{compiled.tree}</TestComponent>
).toJSON();

expect(tree).toMatchSnapshot();
});

it('should allow overriding block code element with components version', () => {
const compile = marksyComponents({
createElement,
elements: {
code({language, code}) {
return <div>{language}: {code}</div>
}
}
});
const compiled = compile('```js\ncode\n```');

const tree = renderer.create(
<TestComponent>{compiled.tree}</TestComponent>
).toJSON();

expect(tree).toMatchSnapshot();
});

it('should highlight code with highlight.js', () => {
const compile = marksy({
createElement,
Expand Down