Using intricate document text as the input parameter in Vue: A guide

I am currently working with this Vue code snippet:

<script>
var starting_point =  '{{ form.content.data }}';
vm = new Vue({
  el: '#editor',
  data: {
    input: starting_point
  },
  computed: {
    compiledMarkdown: function () {
        console.log(marked(this.input,{sanitize:true}));
      return marked(this.input, { sanitize: true });
    }
  },
  methods: {
    update: _.debounce(function (e) {
      this.input = e.target.value
    }, 300)
  }
});

This code is based on the basic example found in the Vue.js documentation: https://v2.vuejs.org/v2/examples/

While it functions well for shorter sentences, I have encountered issues when dealing with longer blog posts that contain custom elements like emojis and line breaks. The JavaScript variable seems to break in such cases. For example, I observe output similar to this:

var starting_point =  '# fourthwhat is this? 
;o
how bout dat. ';

And I receive an error message:

2:40 Uncaught SyntaxError: Invalid or unexpected token
at the end of the line with '?', possibly due to a line break or similar issue.

Therefore, I am exploring options to properly stringify or sanitize the content so that it can be safely used as a JavaScript variable within Vue. Any suggestions on how to achieve this and ensure Vue can interpret the content correctly?

Answer №1

The issue arises due to the absence of escaping the newline character. In other words, instead of:

var starting_point =  '# fourthwhat is this? 
;o
how bout dat. ';

Your code should be written like this:

var starting_point =  '# fourthwhat is this? \
;o \
how bout dat. ';

If you are receiving the markdown string from a dynamic external source, you will need to include the \ character before each line break.

UPDATE: Alternatively, you can use the backtick operator (`) like so:

var starting_point =  `# fourthwhat is this? 
;o
how bout dat. `;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue accessing member value in inherited class constructor in Typescript

My situation involves a class named A, with another class named B that is inherited from it. class A { constructor(){ this.init(); } init(){} } class B extends A { private myMember = {value:1}; constructor(){ super(); ...

Storing query for later utilization using form and javascript

I have implemented a function to create charts using the following code snippet: $('#button_submit').click(function() { var start_date = $('#start_date').val(); var end_date = $('#end_date').val(); var type = $( ...

Sundays and last days are excluding React-big-calendar and dayjs longer events from being displayed

I've encountered a bug in my calendar view implementation. Long events are not displaying on Sundays or the ending day. Please refer to this image for reference: https://i.stack.imgur.com/V0iis.png Event details: Start time: Mon Aug 07 2023 15:44:00 ...

Show the button when the mouse moves over the image

Here is the snippet of code I am working with: <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> <script src="Js/jquery.min.js"></script> <script type="text/javascript"> $(document).r ...

Troubleshooting the display of API-generated lists in Angular 8

I am encountering an issue in Angular 8 when trying to display my list on a page. Below is the code from my proposal-component.ts file: import { Component, OnInit, Input } from "@angular/core"; import { ActivatedRoute, Params } from "@angular/router"; imp ...

Insert title into PDF document

I am currently working on a react application that generates PDF documents easily. The libraries I have utilized are: jspdf html2canvas Below is the code snippet that demonstrates my approach: index.js: <div className="App"> < ...

Attempting to showcase a PDF document within a web browser

I am a newcomer to JavaScript and I'm encountering an issue with displaying a PDF file in the browser. Every time I try to do so, I keep receiving the same error message 'Cannot GET...' Despite trying various methods... router.get("/en ...

How to display content in separate divs using jQuery hover functionality

I'm in the process of creating my online portfolio by using bootstrap and jquery. I have a series of images arranged side by side with each other, and I want to make the description for each image appear when that specific image is hovered over. Each ...

Swapping out data within a container

I've been experimenting with creating a hangman game using JavaScript and HTML. However, I'm facing an issue where clicking on a letter doesn't replace the "_" placeholder. var myList=["Computer","Algorithm","Software","Programming","Develop ...

Using AJAX to Send Requests to PHP

Embarking on my first ajax project, I believe I am close to resolving an issue but require some guidance. The webpage file below features an input field where users can enter their email address. Upon submission, the ajax doWork() function should trigger t ...

In PHP, how can one separate the objects of an array based on their JSON year?

Is there a way to split an array by year based on the date key in ascending order? I've attempted various methods, but haven't been successful. [ { "id": "47", "date": "07/16/2022", "text& ...

Enhancing Security with Subresource Integrity in Angular-Cli

Has anyone discovered a way to enable Subresource Integrity with Angular-CLI? I came across this GitHub Pull Request that suggests it may become a feature in the future: GitHub Pull Request. I tried to activate it on the current versions but had no luck. ...

js extracting information from the XML response using response.text()

Currently, I am sending an ajax request to an API that returns data in XML format. Upon receiving the responseXml data, it gets displayed, but I'm unsure about how to parse it and access specific data elements such as item.line or item.origTime. Shou ...

Converting an array to a JSON object using JavaScript

I need help with converting the following array into a JSON object: var input = [ 'animal/mammal/dog', 'animal/mammal/cat/tiger', 'animal/mammal/cat/lion', 'animal/mammal/elephant', 'animal/ ...

AngularJS - Triggering functions on image load event

I have been on a quest to find the best way to handle the onload event for images in Angular using jqLite. I came across this question, but I am looking for a solution that involves directives. Therefore, the approach below is not satisfactory to me: .c ...

Is it possible to conceal the source code within the dist js file using Vue JS?

I am looking to create a detailed logging page that showcases the logs without revealing the specific type of logging. I want to prevent users from accessing the minified vue JS script and easily reading the logged information. Is there a way to implemen ...

JavaScript loop to target a specific item

My goal is to animate the correct div under each navigation item, rather than all of them with the "navItemUnder" class. You can see exactly what I mean by hovering over a navigation item in this codePen. I am looking for a solution to target only one lin ...

Utilizing Media Queries with Dynamic Vue Properties

On one of my website pages, I always have a Div element with a Background Image that changes based on Media Queries (for example, I fetch a smaller resolution from my CDN on mobile phones). However, since I retrieve the Image URL on Page Load, I need to s ...

Execute an asynchronous function in Javascript, then output the returned data to the console

Is there a way to effectively handle the data returned from an async function? example: JS FILE: async function getData(){ try { $.getJSON('./data.json', (data) => { return data; }); } catch(error ...

Exploring new possibilities in ChartJS with the use of multiple Y

I have successfully created a line chart using Chart.js with two datasets, each having its own Y scale and axis. The code for my datasets and options is as follows: datasets: [{ fill:false, label: 'Heat', yAxisID: "y-axis-1", da ...