eknoor4197
eknoor4197

Reputation: 199

Updating text in markdown

Coding a blog website, using SimpleMDE (Markdown editor) for writing Markdown.

In app.js ,

//EDIT BLOG - FORM
app.get("/blog/:id/edit", function(req,res) {
    Blog.findById(req.params.id, function(err, foundBlog) {
        if(err) {
            res.redirect("/blog");
        } else {
            res.render("editBlog", {blog : foundBlog});
        }
    })
})

//UPDATE BLOG
app.put("/blog/:id", function(req,res) {
    req.body.blog.body = req.sanitize(req.body.blog.body);
    var id = req.params.id;
    Blog.findByIdAndUpdate(req.params.id, req.body.blog,{new: true}, function(err,updatedBlog) {
        if(err) {
            res.redirect("/blog");
        } else {
            res.render("showBlog", {blog : updatedBlog});
})

On using SimpleMDE, it works fine. enter image description here But on clicking the "Edit" button, the changes are saved as such: enter image description here

In editBlog.ejs,

    <div class="field">
    <label>Text</label>
    <textarea id="MyID" type="text" name="blog[body]"><%= blog.body %> </textarea>
  </div>

In showBlog.ejs,

<div id="main-blog-content"><%- blog.body %></div>

Am I missing something here?

Upvotes: 0

Views: 486

Answers (1)

Wes Cossick
Wes Cossick

Reputation: 2933

SimpleMDE is designed for writing Markdown. As such, the editor will provide Markdown when a form is submitted or when the content is accessed. If you're looking to convert it to HTML, you'll need to do so with a parsing library. SimpleMDE uses marked, if you want to maintain consistency with the preview window.

Here's how to use SimpleMDE's built-in parser to convert Markdown to HTML:

var simplemde = new SimpleMDE();
var html = simplemde.markdown("**Example** text");

Upvotes: 0

Related Questions