Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overwrites autoloader path #3808

Open
oddegen opened this issue Jul 11, 2024 · 2 comments
Open

Overwrites autoloader path #3808

oddegen opened this issue Jul 11, 2024 · 2 comments

Comments

@oddegen
Copy link

oddegen commented Jul 11, 2024

Information:

  • Prism version: 1.29.0
  • Plugins: 'autoloader'
  • Environment: Browser, Nextjs

Description
Overwrites autoloader path to the default one, although I've set the new path
image

"use client";

import { Textarea } from "@/components/ui/textarea";
import { KeyboardEvent, useEffect, useRef } from "react";
import Prism from "prismjs";

export function Editor({ lang, value }: { lang: string; value: string }) {
  const PRISM_CDN = "https://cdn.jsdelivr.net/npm/[email protected]";

  useEffect(() => {
    Prism.plugins.autoloader.languages_path = `${PRISM_CDN}/components/`;
  }, []);

  const textareaRef = useRef<HTMLTextAreaElement | null>(null);
  const codeRef = useRef<HTMLElement | null>(null);
  const preRef = useRef<HTMLPreElement | null>(null);

  function handleInput(value: string) {
    if (!codeRef || !codeRef.current) return;

    if (value[value.length - 1] == "\n") {
      value += " ";
    }

    codeRef.current.innerHTML = Prism.highlight(
      value,
      Prism.languages[lang],
      lang
    );

    handleScroll();
  }

  return (
    <div className="h-full w-full">
      <Textarea
        className="h-full w-full p-8 sm:p-14 border-none rounded-none focus-visible:ring-0 focus-visible:ring-offset-0 z-10 text-transparent bg-transparent caret-black dark:caret-white resize-none editing"
        ref={textareaRef}
        onInput={(e) => handleInput(e.target.value)}
        onScroll={handleScroll}
        onKeyDown={(e) => handleKeyDown(e)}
        placeholder="Start typing..."
        spellCheck={false}
      ></Textarea>

      <pre
        aria-hidden="true"
        className={`h-full w-full !p-8 sm:!p-14 z-0 highlighting language-${lang} !bg-transparent`}
        ref={preRef}
        data-autoloader-path={`${PRISM_CDN}/components/`}
      >
        <code
          ref={codeRef}
          className={`language-${lang} !bg-transparent`}
        ></code>
      </pre>
    </div>
  );
}

.babelrc

{
  "presets": ["next/babel"],
    "plugins": [
      [
        "prismjs",
        {
          "languages": [
            "javascript",
            "css",
            "markup"
          ],
          "plugins": [
            "autoloader"
          ],
          "theme": "tomorrow",
          "css": true
        }
      ]
    ]
}
@mAAdhaTTah
Copy link
Member

The useEffect is probably too late relative to the autoloading being run by Prism.

@oddegen
Copy link
Author

oddegen commented Jul 11, 2024

The useEffect is probably too late relative to the autoloading being run by Prism.

But the autoloader object is undefined on the server, so I can't set the path

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants