Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import Prism from 'prismjs'
import React, { useState, useCallback, useMemo } from 'react'
import { Slate, Editable, withReact } from 'slate-react'
import { Text, createEditor } from 'slate'
import { withHistory } from 'slate-history'
import { css } from 'emotion'
// eslint-disable-next-line
;Prism.languages.markdown=Prism.languages.extend("markup",{}),Prism.languages.insertBefore("markdown","prolog",{blockquote:{pattern:/^>(?:[\t ]*>)*/m,alias:"punctuation"},code:[{pattern:/^(?: {4}|\t).+/m,alias:"keyword"},{pattern:/``.+?``|`[^`\n]+`/,alias:"keyword"}],title:[{pattern:/\w+.*(?:\r?\n|\r)(?:==+|--+)/,alias:"important",inside:{punctuation:/==+$|--+$/}},{pattern:/(^\s*)#+.+/m,lookbehind:!0,alias:"important",inside:{punctuation:/^#+|#+$/}}],hr:{pattern:/(^\s*)([*-])([\t ]*\2){2,}(?=\s*$)/m,lookbehind:!0,alias:"punctuation"},list:{pattern:/(^\s*)(?:[*+-]|\d+\.)(?=[\t ].)/m,lookbehind:!0,alias:"punctuation"},"url-reference":{pattern:/!?\[[^\]]+\]:[\t ]+(?:\S+|<(?:\\.|[^>\\])+>)(?:[\t ]+(?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\((?:\\.|[^)\\])*\)))?/,inside:{variable:{pattern:/^(!?\[)[^\]]+/,lookbehind:!0},string:/(?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\((?:\\.|[^)\\])*\))$/,punctuation:/^[\[\]!:]|[<>]/},alias:"url"},bold:{pattern:/(^|[^\\])(\*\*|__)(?:(?:\r?\n|\r)(?!\r?\n|\r)|.)+?\2/,lookbehind:!0,inside:{punctuation:/^\*\*|^__|\*\*$|__$/}},italic:{pattern:/(^|[^\\])([*_])(?:(?:\r?\n|\r)(?!\r?\n|\r)|.)+?\2/,lookbehind:!0,inside:{punctuation:/^[*_]|[*_]$/}},url:{pattern:/!?\[[^\]]+\](?:\([^\s)]+(?:[\t ]+"(?:\\.|[^"\\])*")?\)| ?\[[^\]\n]*\])/,inside:{variable:{pattern:/(!?\[)[^\]]+(?=\]$)/,lookbehind:!0},string:{pattern:/"(?:\\.|[^"\\])*"(?=\)$)/}}}}),Prism.languages.markdown.bold.inside.url=Prism.util.clone(Prism.languages.markdown.url),Prism.languages.markdown.italic.inside.url=Prism.util.clone(Prism.languages.markdown.url),Prism.languages.markdown.bold.inside.italic=Prism.util.clone(Prism.languages.markdown.italic),Prism.languages.markdown.italic.inside.bold=Prism.util.clone(Prism.languages.markdown.bold); // prettier-ignore
const MarkdownPreviewExample = () => {
const [value, setValue] = useState(initialValue)
const renderLeaf = useCallback(props => , [])
const editor = useMemo(() => withHistory(withReact(createEditor())), [])
const decorate = useCallback(([node, path]) => {
const ranges = []
if (!Text.isText(node)) {
return ranges
}
const getLength = token => {
if (typeof token === 'string') {
return token.length
} else if (typeof token.content === 'string') {
(function addJSXSupport() {
let javascript = Prism.util.clone(Prism.languages.javascript);
Prism.languages.jsx = Prism.languages.extend('markup', javascript);
Prism.languages.jsx.tag.pattern = /<\/?[\w.:-]+\s*(?:\s+[\w.:-]+(?:=(?:("|')(\\?[\w\W])*?\1|[^\s'">=]+|(\{[\w\W]*?})))?\s*)*\/?>/i;
Prism.languages.jsx.tag.inside['attr-value'].pattern = /=[^{](?:('|")[\w\W]*?(\1)|[^\s>]+)/i;
let jsxExpression = Prism.util.clone(Prism.languages.jsx);
delete jsxExpression.punctuation;
jsxExpression = Prism.languages.insertBefore('jsx', 'operator', {
'punctuation': /=(?={)|[{}[\];(),.:]/
}, { jsx: jsxExpression });
Prism.languages.insertBefore('inside', 'attr-value', {
'script': {
// Allow for one level of nesting
pattern: /=(\{(?:\{[^}]*}|[^}])+})/i,
inside: jsxExpression,
'alias': 'language-javascript'
}
}, Prism.languages.jsx.tag);
})();
return token;
} else if (typeof token === "string") {
counter.current++;
return new Prism.Token(
"free-text",
token,
["token-" + counter.current, "token-leaf"],
token
);
} else if (Prism.util.type(token.content) === "Array") {
token.content = addCustomTokens(token.content, counter);
return token;
} else {
counter.current++;
const aliases =
Prism.util.type(token.alias) === "Array" ? token.alias : [token.alias];
aliases.push("token-" + counter.current, "token-leaf");
token.alias = aliases;
return token;
}
}
inside: {
variable: { pattern: /(!?\[)[^\]]+(?=\]$)/, lookbehind: !0 },
string: { pattern: /"(?:\\.|[^"\\])*"(?=\)$)/ },
},
},
}),
(Prism.languages.markdown.bold.inside.url = Prism.util.clone(
Prism.languages.markdown.url,
)),
(Prism.languages.markdown.italic.inside.url = Prism.util.clone(
Prism.languages.markdown.url,
)),
(Prism.languages.markdown.bold.inside.italic = Prism.util.clone(
Prism.languages.markdown.italic,
)),
(Prism.languages.markdown.italic.inside.bold = Prism.util.clone(
Prism.languages.markdown.bold,
));
export default Editor;
Object.entries(Prism.languages).forEach(([name, language]) => {
if (Prism.util.type(language) === 'Object') {
insideFences[`language-${name}`] = {
pattern: new RegExp(`(\`\`\`|~~~)${name}\\W[\\s\\S]*`),
inside: {
'cl cl-pre': /(```|~~~).*/,
rest: language,
},
};
}
});
url: {
pattern: /!?\[[^\]]+\](?:\([^\s)]+(?:[\t ]+"(?:\\.|[^"\\])*")?\)| ?\[[^\]\n]*\])/,
inside: {
variable: {
pattern: /(!?\[)[^\]]+(?=\]$)/,
lookbehind: true
},
string: {
pattern: /"(?:\\.|[^"\\])*"(?=\)$)/
}
}
}
});
Prism.languages.markdown.bold.inside.url = Prism.util.clone(Prism.languages.markdown.url);
Prism.languages.markdown.italic.inside.url = Prism.util.clone(Prism.languages.markdown.url);
Prism.languages.markdown.bold.inside.italic = Prism.util.clone(Prism.languages.markdown.italic);
Prism.languages.markdown.italic.inside.bold = Prism.util.clone(Prism.languages.markdown.bold);
/**
* Define a decorator for markdown styles.
*
* @param {Text} text
* @param {Block} block
*/
function addMarks(characters, tokens, offset) {
let updatedOffset = offset;
for (const token of tokens) {
if (typeof token === 'string') {
updatedOffset += token.length;
const highlight = code => {
const grammar = Prism.languages["jsx"];
const language = null;
const env = {
code: code,
grammar: grammar,
language: language
};
Prism.hooks.run("before-tokenize", env);
env.tokens = Prism.tokenize(env.code, env.grammar);
Prism.hooks.run("after-tokenize", env);
env.tokens = addCustomTokens(env.tokens);
return Prism.Token.stringify(Prism.util.encode(env.tokens), env.language);
};