lwzm
11/23/2016 - 2:36 AM

markdown editor

markdown editor

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="chrome=1, IE=edge">

<title>Markdown Editor</title>

<style>
html,
body,
#editor {
    margin: 0;
    height: 100%;
    font-family: 'Helvetica Neue', Arial, sans-serif;
    color: #333;
}

#editor > * {
    display: inline-block;
    width: 49%;
    height: 100%;
    vertical-align: top;
    box-sizing: border-box;
    padding: 0 20px;
}

#editor textarea {
    border: none;
    border-right: 1px solid #ccc;
    resize: none;
    outline: none;
    background-color: #f6f6f6;
    font-size: 14px;
    font-family: 'Monaco', courier, monospace;
    padding: 20px;
	position: fixed;
	left: 49%;
}

code {
    color: #f66;
}

</style>

</head>

<body>

<div id="editor">
    <textarea :value="input" @input="update"></textarea>
    <div v-html="compiledMarkdown"></div>
</div>

<script src="//unpkg.com/marked@latest"></script>
<script src="//unpkg.com/lodash@latest"></script>
<script src="//unpkg.com/vue@latest/dist/vue.js"></script>
<script>
new Vue({
    el: '#editor',
    data: {
        input: localStorage.getItem(".markdown") || '# hello'
    },
    computed: {
        compiledMarkdown: function () {
            return marked(this.input, { sanitize: true })
        }
    },
    methods: {
        update: _.debounce(function (e) {
            localStorage.setItem(".markdown", this.input = e.target.value)
        }, 100)
    }
})
</script>

</body>
</html>