Exploring the potential of Wysimark using traditional JavaScriptcoding techniques

Have you heard of Wysimark? It's a WYSIWYG Markdown Editor that I'm trying to add to my webpage using plain JavaScript and the <script> tag.

I installed it by running pnpm add @wysimark/standalone

Oddly enough, the documentation doesn't seem to provide instructions on how to add the editor using the script tag, even though it claims to support "Plain JS" (or maybe there's something else missing?)

This is what I have tried:

index.html

<html>
    <body>
        <div id="editor-container"></div>
        <script defer type=module src='index.js'></script>
    </body>
</html>

index.js

import { createWysimark } from "@wysimark/standalone"

const container = document.getElementById("editor-container")

const wysimark = createWysimark(container, {
  initialMarkdown: "# Hello World",
})

But unfortunately, an error occurs:

Uncaught SyntaxError: The requested module '/cmwysiwig/node_modules/@wysimark/standalone/.dist/browser/index.cjs.js' does not provide an export named 'createWysimark' (at index.js:1:10)

What am I doing wrong here?

Answer №1

The root of this problem lies in the fact that the package does not expose the createWysimark function in the CommonJS (CJS) build, and the ESM build does not properly bundle the imports. To resolve this issue, you can utilize a bundler to repackage the code as shown below:

import { buildForBrowser } from '@jsheaven/easybuild'

await buildForBrowser({
  entryPoint: '$theWysimarkStandaloneDistFile.js', // still having import statements
  outfile: './dist/javascript/index.js', // imports are resolved/bundled in now; this file can be used directly via <script> inject; but still needs to be shipped via a CDN or webserver
  dts: false,
  debug: process.argv.indexOf('--dev') > -1,
  esBuildOptions: {
    logLevel: 'error',
  },
})

Currently, the author has various unresolved issues on their repository related to this matter. As a temporary solution, you can make use of the wysimark-standalone npm package, which addresses this exact problem. The resulting bundled code can then be loaded directly via unpkg:

<html>
  <head>
    <script src="https://www.unpkg.com/wysimark-standalone/dist/javascript/index.cjs.js"></script>
  </head>
  <body>
    <div id="editor-container"></div>
    <script>
      const container = document.getElementById("editor-container")
      // createWysimark is available on window now
      const wysimark = createWysimark(container, {
        initialMarkdown: "# Hello World",
      })
    </script>
  </body>
</html>

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

Creating a Singular Instance for Dynamically Loaded Module in Next.js

I'm currently working on creating a Singleton instance for a dynamically imported module in my Next.js app. However, the problem is that each time I call getInstance, it initializes a new instance instead of reusing the existing one. The following co ...

Using jQuery to retrieve AJAX data and confirm submission

My objective is to trigger an AJAX call when a form is submitted, retrieve some GET result, and then prompt for confirmation: $('#form1').submit(function() { var messages = ... // get result from $.get() call return confirm("Confirm dele ...

The async function in React encountered an unexpected reserved word 'await'

Hi there! I am relatively new to React and I am trying to figure out how to retrieve location coordinates and send them to an API with certain parameters. However, I keep encountering this error message: Unexpected reserved word 'await' Here is ...

Navigating with UI-Router within a single view for both viewing and editing purposes

I have been encountering an issue with changing states between the view page and edit page using UI-Router. Here are some of the things I have attempted: Controller: $stateProvider .state('showViewA', { views: { ...

Exploring JavaScript's capability to parse JSON data

Having some trouble extracting information from the Google Maps API into my application using JavaScript. I'm struggling to figure out how to access the data in the returned JSON object. var site = "./maps/scripts/reverseGeocode/locale.php"; ...

The submission of the form is not functioning correctly when triggered by JavaScript using a button

My website was designed using a CSS/HTML framework that has been seamlessly integrated into an ASP.NET site. Within a ContentPlaceHolder, I have implemented a basic login form. The unique aspect is that I am utilizing the onclick event of an image to subm ...

The function passport.authenticate does not exist / User.findone is not a valid function

**I'm really struggling to figure out what I'm doing wrong. I'm new to passport and JavaScript in general, so any help would be appreciated! Every time I try to run it, I get an error saying 'authenticate is not a function'. Then, ...

What could be causing my jQuery code to not function properly?

I wrote a small piece of code in jQuery, but I am having trouble executing it. Can someone help me figure out what I'm doing wrong? <html> <head> <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.min.js"> & ...

What's the best way to incorporate mouseenter() and mouseleave() into several elements sharing the same class?

I have a vision for a fun game where players must navigate from one platform to another without falling off the edges. The game starts when you hover over the initial platform, and success is achieved by reaching the final platform. However, failure occurs ...

The young ones whose height is 80% that of their taller parent's

Achieving the Desired Height In order to set a div with 80% of the height of its parent element without a defined height, I am aiming for a responsive effect. The Layered Layout Issue Within my layout consisting of five layers - .header, .site, .contain ...

Injecting CSS styles into the head tag dynamically with jQuery from within the body of a

When it comes to adding CSS to the head of an HTML page from the body using JavaScript and jQuery, I have come across two methods. One involves using jQuery to directly append the CSS to the head, while the other method involves creating a style element an ...

Sending Parameters Using Higher Order Functions

Utilizing React and attempting to pass multiple arguments along with the 'event', I opted to implement a Higher Order function for this purpose. Unfortunately, it seems that the 'id' passed to the Higher Order function is not being rec ...

Instructions for resetting a DataTable in JQuery that was originally built using an append operation

Encountered a problem with restarting my datatable after populating it with data from a web service. Everything fills up correctly, but upon clicking the Search Button again, the old table header remains and the function fails to include the new results in ...

Troubleshooting: JQuery is not properly adding the CSS value

I have a piece of code that adds a CSS class to an element once the user scrolls to a specific point on the page. This is intended to create a sticky menu bar. The distance to scroll is determined based on the screen resolution and is calculated using JQue ...

What is the solution to having a div move along with window resizing without displacing adjacent divs?

After much effort, I still can't seem to get this working correctly. I've been playing around with a section named "RightExtra" and a div inside it called "RightExtraContent". My goal is to allow these two divs to move freely when the window is ...

My goal is to ensure that when a user copies the URL of a website from the address bar and pastes it into a social media app, the link automatically transforms into the title of

Similar to the process in Airbnb where a copied URL is pasted in Slack and replaced with specific text along with the site URL I have tried adding og:title, description, and images, but cannot figure out how to handle this issue. Any assistance would be a ...

Jest fails to pass when encountering the double colon

Having issues testing a React app using Jest. I encounter errors when running my code: FAIL src\App.test.js ● Test suite failed to run C:/Users/user1/Projects/map-editor/src/App.js: Unexpected token (40:33) 38 | <div cla ...

Tips on effectively rendering child components conditionally in React

My components currently consist of an AddBookPanel containing the AddBookForm. I am looking to implement a feature where the form is displayed upon clicking the 'AddBookButton', and hidden when the 'x' button (image within AddBookForm c ...

Position a div relative to another div with a static position

I am attempting to create a site that is fullscreen and responsive, but I am having issues with elements in the container overflowing on smaller screens, causing it to not be 100% as desired. I have tried using: top:100%; position:relative; width:100%; he ...

Changing the color of a placeholder using Javascript: A step-by-step guide

I've been searching online without any luck so far. I'm attempting to change the placeholder color of a text box using JavaScript, but I'm not sure how to go about it. I already have a color picker that changes colors. If my CSS looks somet ...