Repository: christianalfoni/markdown-to-react-components Branch: master Commit: b9b083c0ed4c Files: 11 Total size: 21.1 KB Directory structure: gitextract_gwo92svq/ ├── .gitignore ├── ISSUE_TEMPLATE.md ├── LICENSE ├── README.md ├── app/ │ ├── main.js │ ├── prism.css │ └── prism.js ├── package.json ├── src/ │ ├── CodeComponent.js │ └── index.js └── webpack.config.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ node_modules ================================================ FILE: ISSUE_TEMPLATE.md ================================================ This repo is DEPRECATED, please create issues over at: https://github.com/cerebral/marksy ================================================ FILE: LICENSE ================================================ The MIT License (MIT) Copyright (c) 2015 Christian Alfoni Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # markdown-to-react-components Convert markdown into react components ## DEPRECATED This repo is DEPRECATED, please go to: https://github.com/cerebral/marksy ================================================ FILE: app/main.js ================================================ require('./prism.css'); require('./prism.js'); var React = require('react'); var lib = require('markdown-to-react-components'); var DOM = React.DOM; var HalfColumn = { width: '50%', verticalAlign: 'top', display: 'inline-block' }; lib.configure({ h1: React.createClass({ render: function () { return DOM.h1({ style: {color: 'red'} }, this.props.children) } }) }); var App = React.createClass({ getInitialState: function () { return { tree: null }; }, onTextareaChange: function (event) { this.setState({ tree: lib(event.target.value).tree }); }, render: function () { return DOM.div(null, DOM.div({ style: HalfColumn }, this.state.tree), DOM.textarea({ style: { width: 500, height: 500 }, onChange: this.onTextareaChange }) ); } }); React.render(React.createElement(App), document.body); ================================================ FILE: app/prism.css ================================================ /* http://prismjs.com/download.html?themes=prism&languages=markup+clike+javascript+jsx */ /** * prism.js default theme for JavaScript, CSS and HTML * Based on dabblet (http://dabblet.com) * @author Lea Verou */ code[class*="language-"], pre[class*="language-"] { color: black; background: none; text-shadow: 0 1px white; font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; word-wrap: normal; line-height: 1.5; -moz-tab-size: 4; -o-tab-size: 4; tab-size: 4; -webkit-hyphens: none; -moz-hyphens: none; -ms-hyphens: none; hyphens: none; } pre[class*="language-"]::-moz-selection, pre[class*="language-"] ::-moz-selection, code[class*="language-"]::-moz-selection, code[class*="language-"] ::-moz-selection { text-shadow: none; background: #b3d4fc; } pre[class*="language-"]::selection, pre[class*="language-"] ::selection, code[class*="language-"]::selection, code[class*="language-"] ::selection { text-shadow: none; background: #b3d4fc; } @media print { code[class*="language-"], pre[class*="language-"] { text-shadow: none; } } /* Code blocks */ pre[class*="language-"] { padding: 1em; margin: .5em 0; overflow: auto; } :not(pre) > code[class*="language-"], pre[class*="language-"] { background: #f5f2f0; } /* Inline code */ :not(pre) > code[class*="language-"] { padding: .1em; border-radius: .3em; white-space: normal; } .token.comment, .token.prolog, .token.doctype, .token.cdata { color: slategray; } .token.punctuation { color: #999; } .namespace { opacity: .7; } .token.property, .token.tag, .token.boolean, .token.number, .token.constant, .token.symbol, .token.deleted { color: #905; } .token.selector, .token.attr-name, .token.string, .token.char, .token.builtin, .token.inserted { color: #690; } .token.operator, .token.entity, .token.url, .language-css .token.string, .style .token.string { color: #a67f59; background: hsla(0, 0%, 100%, .5); } .token.atrule, .token.attr-value, .token.keyword { color: #07a; } .token.function { color: #DD4A68; } .token.regex, .token.important, .token.variable { color: #e90; } .token.important, .token.bold { font-weight: bold; } .token.italic { font-style: italic; } .token.entity { cursor: help; } ================================================ FILE: app/prism.js ================================================ /* http://prismjs.com/download.html?themes=prism&languages=markup+clike+javascript+jsx */ var _self="undefined"!=typeof window?window:"undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope?self:{},Prism=function(){var e=/\blang(?:uage)?-(\w+)\b/i,t=_self.Prism={util:{encode:function(e){return e instanceof n?new n(e.type,t.util.encode(e.content),e.alias):"Array"===t.util.type(e)?e.map(t.util.encode):e.replace(/&/g,"&").replace(/e.length)break e;if(!(d instanceof a)){u.lastIndex=0;var m=u.exec(d);if(m){g&&(f=m[1].length);var y=m.index-1+f,m=m[0].slice(f),v=m.length,k=y+v,b=d.slice(0,y+1),w=d.slice(k+1),P=[p,1];b&&P.push(b);var A=new a(i,c?t.tokenize(m,c):m,h);P.push(A),w&&P.push(w),Array.prototype.splice.apply(r,P)}}}}}return r},hooks:{all:{},add:function(e,n){var a=t.hooks.all;a[e]=a[e]||[],a[e].push(n)},run:function(e,n){var a=t.hooks.all[e];if(a&&a.length)for(var r,l=0;r=a[l++];)r(n)}}},n=t.Token=function(e,t,n){this.type=e,this.content=t,this.alias=n};if(n.stringify=function(e,a,r){if("string"==typeof e)return e;if("Array"===t.util.type(e))return e.map(function(t){return n.stringify(t,a,e)}).join("");var l={type:e.type,content:n.stringify(e.content,a,r),tag:"span",classes:["token",e.type],attributes:{},language:a,parent:r};if("comment"==l.type&&(l.attributes.spellcheck="true"),e.alias){var i="Array"===t.util.type(e.alias)?e.alias:[e.alias];Array.prototype.push.apply(l.classes,i)}t.hooks.run("wrap",l);var o="";for(var s in l.attributes)o+=(o?" ":"")+s+'="'+(l.attributes[s]||"")+'"';return"<"+l.tag+' class="'+l.classes.join(" ")+'" '+o+">"+l.content+""},!_self.document)return _self.addEventListener?(_self.addEventListener("message",function(e){var n=JSON.parse(e.data),a=n.language,r=n.code,l=n.immediateClose;_self.postMessage(t.highlight(r,t.languages[a],a)),l&&_self.close()},!1),_self.Prism):_self.Prism;var a=document.currentScript||[].slice.call(document.getElementsByTagName("script")).pop();return a&&(t.filename=a.src,document.addEventListener&&!a.hasAttribute("data-manual")&&document.addEventListener("DOMContentLoaded",t.highlightAll)),_self.Prism}();"undefined"!=typeof module&&module.exports&&(module.exports=Prism),"undefined"!=typeof global&&(global.Prism=Prism); Prism.languages.markup={comment://,prolog:/<\?[\w\W]+?\?>/,doctype://,cdata://i,tag:{pattern:/<\/?(?!\d)[^\s>\/=.$<]+(?:\s+[^\s>\/=]+(?:=(?:("|')(?:\\\1|\\?(?!\1)[\w\W])*\1|[^\s'">=]+))?)*\s*\/?>/i,inside:{tag:{pattern:/^<\/?[^\s>\/]+/i,inside:{punctuation:/^<\/?/,namespace:/^[^\s>\/:]+:/}},"attr-value":{pattern:/=(?:('|")[\w\W]*?(\1)|[^\s>]+)/i,inside:{punctuation:/[=>"']/}},punctuation:/\/?>/,"attr-name":{pattern:/[^\s>\/]+/,inside:{namespace:/^[^\s>\/:]+:/}}}},entity:/&#?[\da-z]{1,8};/i},Prism.hooks.add("wrap",function(a){"entity"===a.type&&(a.attributes.title=a.content.replace(/&/,"&"))}),Prism.languages.xml=Prism.languages.markup,Prism.languages.html=Prism.languages.markup,Prism.languages.mathml=Prism.languages.markup,Prism.languages.svg=Prism.languages.markup; Prism.languages.clike={comment:[{pattern:/(^|[^\\])\/\*[\w\W]*?\*\//,lookbehind:!0},{pattern:/(^|[^\\:])\/\/.*/,lookbehind:!0}],string:/(["'])(\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,"class-name":{pattern:/((?:\b(?:class|interface|extends|implements|trait|instanceof|new)\s+)|(?:catch\s+\())[a-z0-9_\.\\]+/i,lookbehind:!0,inside:{punctuation:/(\.|\\)/}},keyword:/\b(if|else|while|do|for|return|in|instanceof|function|new|try|throw|catch|finally|null|break|continue)\b/,"boolean":/\b(true|false)\b/,"function":/[a-z0-9_]+(?=\()/i,number:/\b-?(?:0x[\da-f]+|\d*\.?\d+(?:e[+-]?\d+)?)\b/i,operator:/--?|\+\+?|!=?=?|<=?|>=?|==?=?|&&?|\|\|?|\?|\*|\/|~|\^|%/,punctuation:/[{}[\];(),.:]/}; Prism.languages.javascript=Prism.languages.extend("clike",{keyword:/\b(as|async|await|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|export|extends|finally|for|from|function|get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|set|static|super|switch|this|throw|try|typeof|var|void|while|with|yield)\b/,number:/\b-?(0x[\dA-Fa-f]+|0b[01]+|0o[0-7]+|\d*\.?\d+([Ee][+-]?\d+)?|NaN|Infinity)\b/,"function":/[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*(?=\()/i}),Prism.languages.insertBefore("javascript","keyword",{regex:{pattern:/(^|[^\/])\/(?!\/)(\[.+?]|\\.|[^\/\\\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})]))/,lookbehind:!0}}),Prism.languages.insertBefore("javascript","class-name",{"template-string":{pattern:/`(?:\\`|\\?[^`])*`/,inside:{interpolation:{pattern:/\$\{[^}]+\}/,inside:{"interpolation-punctuation":{pattern:/^\$\{|\}$/,alias:"punctuation"},rest:Prism.languages.javascript}},string:/[\s\S]+/}}}),Prism.languages.markup&&Prism.languages.insertBefore("markup","tag",{script:{pattern:/()[\w\W]*?(?=<\/script>)/i,lookbehind:!0,inside:Prism.languages.javascript,alias:"language-javascript"}}),Prism.languages.js=Prism.languages.javascript; !function(a){var e=a.util.clone(a.languages.javascript);a.languages.jsx=a.languages.extend("markup",e),a.languages.jsx.tag.pattern=/<\/?[\w\.:-]+\s*(?:\s+[\w\.:-]+(?:=(?:("|')(\\?[\w\W])*?\1|[^\s'">=]+|(\{[\w\W]*?\})))?\s*)*\/?>/i,a.languages.jsx.tag.inside["attr-value"].pattern=/=[^\{](?:('|")[\w\W]*?(\1)|[^\s>]+)/i;var s=a.util.clone(a.languages.jsx);delete s.punctuation,s=a.languages.insertBefore("jsx","operator",{punctuation:/=(?={)|[{}[\];(),.:]/},{jsx:s}),a.languages.insertBefore("inside","attr-value",{script:{pattern:/=(\{(?:\{[^}]*\}|[^}])+\})/i,inside:s,alias:"language-javascript"}},a.languages.jsx.tag)}(Prism); ================================================ FILE: package.json ================================================ { "name": "markdown-to-react-components", "version": "0.2.4", "description": "Convert markdown into react components", "main": "src/index.js", "scripts": { "start": "webpack-dev-server --devtool eval-source-map --progress --colors --content-base build", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", "url": "https://github.com/christianalfoni/markdown-to-react-components" }, "keywords": [ "react", "markdown", "convert" ], "author": "Christian Alfoni", "license": "MIT", "bugs": { "url": "https://github.com/christianalfoni/markdown-to-react-components/issues" }, "homepage": "https://github.com/christianalfoni/markdown-to-react-components", "devDependencies": { "css-loader": "^0.15.2", "json-loader": "^0.5.2", "node-libs-browser": "^0.5.2", "react": "^0.14.7", "style-loader": "^0.12.3", "webpack": "^1.10.0", "webpack-dev-server": "^1.10.1" }, "dependencies": { "he": "^1.1.0", "marked": "^0.3.3" } } ================================================ FILE: src/CodeComponent.js ================================================ var React = require('react'); var CodeComponent = React.createClass({ componentDidMount: function () { if (typeof Prism === 'undefined') { console.warn('You do not have Prism included as a global object'); return; } Prism.highlightAll(); }, componentDidUpdate: function () { if (typeof Prism === 'undefined') { console.warn('You do not have Prism included as a global object'); return; } Prism.highlightAll(); }, render: function () { return React.createElement('pre', {key: this.props.key}, React.createElement('code', { ref: 'code', className: 'language-' + this.props.language }, this.props.code) ); } }); module.exports = CodeComponent; ================================================ FILE: src/index.js ================================================ var React = require('react'); var marked = require('marked'); var he = require('he'); var CodeComponent = React.createFactory(require('./CodeComponent.js')); var renderer = new marked.Renderer(); var options = {}; var inlineIds = 0; var keys = 0; var inlines = {}; var result = []; var toc = []; // Converts inline IDs to actual elements var createBlockContent = function (content) { var textWithInlines = content.split(/(\{\{.*?\}\})/); content = textWithInlines.map(function (text) { var inline = text.match(/\{\{(.*)\}\}/); if (inline) { return inlines[inline[1]]; } else { if (text != '') { return he.decode(text); } } }); return content; }; var getTocPosition = function (toc, level) { var currentLevel = toc.children; while (true) { if (!currentLevel.length || currentLevel[currentLevel.length - 1].level === level) { return currentLevel; } else { currentLevel = currentLevel[currentLevel.length - 1].children; } } }; renderer.code = function (code, language) { var props = { key: keys++, language: language, code: code }; if (options.code) { result.push(React.createElement(options.code, props)); } else { result.push(CodeComponent(props)); } }; renderer.blockquote = function (text) { var count = text.split(/(\{\{.*?\}\})/).filter(function(n){ return n != ""}); count.forEach(function(){ result.pop(); }); result.push(React.createElement(options.blockquote || 'blockquote', {key: keys++}, createBlockContent(text))); }; renderer.html = function (html) { result.push(React.createElement(options.html || React.createClass({ render: function render () { return React.createElement('div', { dangerouslySetInnerHTML: { __html: this.props.html } }); } }), { html: html })); }; renderer.heading = function (text, level) { var type = 'h' + level; type = options[type] || type; var id = text.replace(/\s/g, '-').toLowerCase(); var lastToc = toc[toc.length -1]; if (!lastToc || lastToc.level > level) { toc.push({ id: id, title: text, level: level, children: [] }); } else { var tocPosition = getTocPosition(lastToc, level); tocPosition.push({ id: id, title: text, level: level, children: [] }); } var inId = inlineIds++; inlines[inId] = React.createElement(type, { key: keys++, id: id }, createBlockContent(text)); result.push(inlines[inId]); return '{{' + inId + '}}'; }; renderer.hr = function () { result.push(React.createElement(options.hr || 'hr', {key: keys++})); }; renderer.list = function (body, ordered) { var id = inlineIds++; inlines[id] = React.createElement(ordered ? options.ol || 'ol' : options.ul || 'ul', {key: keys++}, createBlockContent(body)); result.push(inlines[id]); return '{{' + id + '}}'; }; renderer.listitem = function (text) { var id = inlineIds++; inlines[id] = React.createElement(options.li || 'li', {key: keys++}, createBlockContent(text)); return '{{' + id + '}}'; }; renderer.paragraph = function (text) { var id = inlineIds++; inlines[id] = React.createElement(options.p || 'p', {key: keys++}, createBlockContent(text)); result.push(inlines[id]); return '{{' + id + '}}'; }; renderer.table = function (header, body) { var id = inlineIds++; inlines[id] = React.createElement(options.table || 'table', {key: keys++}, React.createElement(options.thead || 'thead', null, createBlockContent(header)), React.createElement(options.tbody || 'tbody', null, createBlockContent(body)) ); result.push(inlines[id]); return '{{' + id + '}}'; }; renderer.thead = function (content) { var id = inlineIds++; inlines[id] = React.createElement(options.thead || 'thead', {key: keys++}, createBlockContent(content)); return '{{' + id + '}}'; }; renderer.tbody = function (content) { var id = inlineIds++; inlines[id] = React.createElement(options.tbody || 'tbody', {key: keys++}, createBlockContent(content)); return '{{' + id + '}}'; }; renderer.tablerow = function (content) { var id = inlineIds++; inlines[id] = React.createElement(options.tr || 'tr', {key: keys++}, createBlockContent(content)); return '{{' + id + '}}'; }; renderer.tablecell = function (content, flags) { var id = inlineIds++; var props = flags.align ? {className: 'text-' + flags.align} : {key: keys++}; inlines[id] = React.createElement(flags.header ? options.th || 'th' : options.td || 'td', props, createBlockContent(content)); return '{{' + id + '}}'; }; renderer.link = function (href, title, text) { var id = inlineIds++; inlines[id] = React.createElement(options.a || 'a', { href: href, title: title, key: keys++, target: 'new' }, createBlockContent(text)); return '{{' + id + '}}'; }; renderer.strong = function (text) { var id = inlineIds++; inlines[id] = React.createElement(options.strong || 'strong', {key: keys++}, createBlockContent(text)); return '{{' + id + '}}'; }; renderer.em = function (text) { var id = inlineIds++; inlines[id] = React.createElement(options.em || 'em', {key: keys++}, createBlockContent(text)); return '{{' + id + '}}'; }; renderer.codespan = function (text) { var id = inlineIds++; inlines[id] = React.createElement(options.codespan || 'code', {key: keys++}, he.decode(text)); return '{{' + id + '}}'; }; renderer.br = function () { var id = inlineIds++; inlines[id] = React.createElement(options.br || 'br', {key: keys++}); return '{{' + id + '}}'; }; renderer.del = function (text) { var id = inlineIds++; inlines[id] = React.createElement(options.del || 'del', {key: keys++}, he.decode(text)); return '{{' + id + '}}'; }; renderer.image = function (href, title, text) { var id = inlineIds++; inlines[id] = React.createElement(options.img || 'img', {src: href, alt: title, key: keys++}); return '{{' + id + '}}'; }; var exec = function (content) { result = []; toc = []; inlines = {}; keys = 0; marked(content, {renderer: renderer, smartypants: true}); return { tree: result, toc: toc }; }; exec.configure = function (newOptions) { options = newOptions; }; module.exports = exec; ================================================ FILE: webpack.config.js ================================================ var path = require('path'); var sourcePath = path.resolve(__dirname, 'src', 'index.js'); var appPath = path.resolve(__dirname, 'app', 'main.js'); module.exports = { entry: [ 'webpack-dev-server/client?http://localhost:8080', appPath ], output: { path: './build', filename: 'bundle.js' }, resolve: { alias: { 'markdown-to-react-components': sourcePath } }, module: { loaders: [{ test: /\.json$/, loader: 'json' }, { test: /\.css$/, loader: 'style!css' }] } };