Tips on maintaining line breaks in VueJS when fetching text from raw-loader and styling it with showdown

I am currently developing a website to showcase fundamental details about a particular sport. The (small) rulebook is stored as rulebook.md in my project. I am utilizing webpack along with the latest vue-cli and yarn for this project.

To import the rulebook, I am employing the use of raw-loader which passes the content to a Vue component named markdown. This component utilizes the showdown module to format the markdown content.

@/components/Markdown.vue

<template>
  <div id="markdown">
    {{ source }}
    {{ converted }}
  </div>
</template>

<script>
import * as showdown from 'showdown'
const converter = new showdown.Converter()
converter.setOption('simpleLineBreaks', true)

export default {
  name: 'markdown',
  props: ['source'],
  data () {
    return {
      converted: converter.makeHtml(this.source)
    }
  }
}
</script>

<style lang="scss" scoped>
.markdown {
  white-space: pre-line;
  word-wrap: break-word;
}
</style>

@/views/punchies/Rulebook.vue

<template>
  <div id="punchies-rulebook" class="container">
    ...
    <markdown ... :source="source"></markdown>
  </div>
</template>

<script>
import Markdown from '@/components/Markdown.vue'
// eslint-disable-next-line
import source from 'raw-loader!@/views/punchies/misc/rulebook.md'

export default {
  name: 'rulebook',
  components: {
    'markdown': Markdown
  },
  data () {
    return {
      source
    }
  }
}
</script>

...

I have attempted to configure showdown to handle line breaks and instructed CSS to display line breaks accordingly. However, when using console.log(source), the rulebook is displayed without any line breaks. After researching this issue, I discovered on the Github page for raw-loader that it does not preserve newlines.

Answer №1

Inside the Markdown component

<template>
    <p>{{content}}</p>
    <p v-html="renderedContent"></p>
</template>

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

Events in d3.js are properly registered, however they are not being triggered as

After creating an enter section that transitions to set the opacity to 1, I encountered an issue where the 'click' event on the circle worked but not on the text. Interestingly, when I replaced the 'text' with a 'rect' and se ...

Using ajax to retrieve and refresh data

I need to perform real-time updates to a column in my table and display the updated data instantly. Here is the code I am using: <html> <body> <div id="output"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3. ...

Encountering the error message "[TypeError]: Unable to access properties of undefined (reading 'prototype')" when utilizing axios post function in next.js

I am encountering an issue while utilizing the next.js app directory and attempting to make a POST request using axios. const submitHandler = async (e) => { e.preventDefault(); try { const {data} = await axios.post('/api/register&ap ...

Just starting out with Vue and hitting a roadblock with global variables

I am currently working on creating a filtered list in Native Vue, specifically compiling and running it for Android. export default { components: { card }, data: { search: 'An', level: "", }, computed: { searchI ...

Instructions on creating an individual DropdownIndicator component with React-select for the purpose of reusability in different sections of the project

The code below functions properly when the DropdownIndicator component is kept in the same file. However, I am interested in separating DropdownIndicator into its own file so that it can be reused. This way, it can be used as a separate component and incl ...

What is the best way to effectively apply a mask within a PixiJS container so that its color does not display upon page refresh?

After implementing the code snippet below to add a mask in a container, I encountered an issue where upon clicking the refresh button on the page (chrome), the pixi stage would turn completely white until the refreshing process is completed. Can anyone p ...

The p-dialog lacks the proper styling and does not show or hide correctly

I am working on adding a feature where dragging and dropping an event in a calendar triggers a dialog box that requires the user to confirm if they want to postpone the event. However, I ran into an issue where the styling of my p-dialog is not defaulting ...

Issue with displaying checkboxes in the dataTable table

I am currently working with dataTables and encountering difficulties displaying checkboxes on my tables. Below is the code that I have: <table id="employeeList" class="table table-sm table-bordered table-hover" cellspacing="0" width="200%"> &l ...

Preserving tinyMCE content prior to sending to PHP server

I am facing a challenge with this task. It functions, however, I have to submit the form twice for the tinyMCE value to be stored. $("form#data").on('submit',function(e){ e.preventDefault(); tinymce.init({ selector: " ...

FETCH API causing unexpected response of [object Object]

Currently, I am utilizing the FETCH API to retrieve a value stored within a json file. The challenge lies in ensuring that this value is correctly assigned to a variable. An issue arises where the variable ends up containing [object Object] as its value. ...

Express receiving undefined data in Ajax POST request

I'm currently facing an issue with sending data to be parsed. The client-side script I have is as follows: function addURL(link) { console.log("Adding url..."); $.ajax({ type: "POST", url: location.protocol + "/ ...

A guide on troubleshooting Angular 2 using webpack

After encountering the following error: EXCEPTION: Uncaught (in promise): TypeError: Cannot set property stack of [object Object] which has only a getter TypeError: Cannot set property stack of [object Object] which has only a getter I am faced with an e ...

Angular form for updating values

In my Angular project, I am utilizing an angular dynamic form where I am populating values using JSON. data = { firstName:"Eliseo", lastName:"Plunker", myArray:[ { emailAddress:"<a href='/cdn-cgi/l/emai ...

Error encountered while using XLSX.write in angular.js: n.t.match function is not recognized

I've encountered an issue while trying to generate an .xlsx file using the XLSX library. The error message I received is as follows: TypeError: n.t.match is not a function at Ps (xlsx.full.min.js:14) at Jd (xlsx.full.min.js:18) at Sv (xlsx.full.min ...

Why does this code snippet throw an error if let is not hoisted or in the temporal dead zone? It could have simply used the global reference instead

var b = 6; { console.log(b) let b = 55 } When running this code snippet, I encounter the following error message: ReferenceError: Cannot access 'b' before initialization Why is the console.log(b) not displaying 6 as expected? ...

Using Tinymce to automatically send form on blur action

Attempting to trigger form submission upon blur event in Tinymce, but encountering difficulties. tinymce.init({ selector: 'textarea.html', menubar:false, force_br_newlines : true, force_p_newlines ...

Controlling various divs with unique identifiers using Angular

Imagine having a vast number of HTML elements with unique IDs that require their styles to be changed repeatedly in a controller for spectrum visualization. What is the most efficient way to achieve this in Angular, without resorting to duplicative HTML co ...

Using JavaScript to make an AJAX call to a different domain while bypassing the Content Security Policy restrictions

While parsing a web page, I need to initiate an AJAX call to my localhost depending on the content. The purpose is to exchange data using a PHP script on my localhost, possibly in JSON format (still under testing). This process is part of a plugin that I ...

Can you help me make a JavaScript Random Number Generator that utilizes HTML input fields and buttons?

I am currently working on developing a random number generator that takes user input through HTML. The idea is to have the user enter two values and then click "submit" to receive a random number within that range. However, I seem to be stuck at this poin ...

"Upon loading an mtl file in three.js, the result appears

I'm encountering an issue while attempting to incorporate a .obj level into my program as it appears black when rendered. The associated .mtl file necessitates multiple images spread throughout, leaving no untextured spaces. Strangely, the same object ...