@memori.ai/memori-react 7.8.5 → 7.8.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,66 +1,85 @@
1
- import React from 'react';
1
+ import React, { useEffect } from 'react';
2
2
  import { Medium } from '@memori.ai/memori-api-client/dist/types';
3
3
  import Button from '../ui/Button';
4
4
  import Copy from '../icons/Copy';
5
5
  import { prismSyntaxLangs } from '../../helpers/constants';
6
- import { PrismAsyncLight as SyntaxHighlighter } from 'react-syntax-highlighter';
7
- import { atomDark } from 'react-syntax-highlighter/dist/cjs/styles/prism';
8
- import tsx from 'react-syntax-highlighter/dist/cjs/languages/prism/tsx';
9
- import json from 'react-syntax-highlighter/dist/cjs/languages/prism/json';
10
- import scss from 'react-syntax-highlighter/dist/cjs/languages/prism/scss';
11
- import bash from 'react-syntax-highlighter/dist/cjs/languages/prism/bash';
12
- import python from 'react-syntax-highlighter/dist/cjs/languages/prism/python';
13
- import cpp from 'react-syntax-highlighter/dist/cjs/languages/prism/cpp';
14
- import php from 'react-syntax-highlighter/dist/cjs/languages/prism/php';
15
- import ruby from 'react-syntax-highlighter/dist/cjs/languages/prism/ruby';
16
- import sql from 'react-syntax-highlighter/dist/cjs/languages/prism/sql';
17
6
  import { useTranslation } from 'react-i18next';
7
+ import cx from 'classnames';
18
8
 
19
9
  export interface Props {
20
10
  medium: Medium;
21
11
  className?: string;
22
12
  preview?: boolean;
23
- showLineNumbers?: boolean;
24
13
  showCopyButton?: boolean;
25
14
  }
26
15
 
27
- // These have to match prismSyntaxLangs
28
- SyntaxHighlighter.registerLanguage('tsx', tsx);
29
- SyntaxHighlighter.registerLanguage('json', json);
30
- SyntaxHighlighter.registerLanguage('scss', scss);
31
- SyntaxHighlighter.registerLanguage('bash', bash);
32
- SyntaxHighlighter.registerLanguage('python', python);
33
- SyntaxHighlighter.registerLanguage('cpp', cpp);
34
- SyntaxHighlighter.registerLanguage('php', php);
35
- SyntaxHighlighter.registerLanguage('ruby', ruby);
36
- SyntaxHighlighter.registerLanguage('sql', sql);
16
+ const loadPrismScripts = () => {
17
+ const existingScript = document.getElementById('memori-prism-script');
18
+ if (existingScript) {
19
+ return;
20
+ }
21
+
22
+ const script = document.createElement('script');
23
+ script.src = 'https://cdn.jsdelivr.net/npm/prismjs@1.29.0/prism.min.js';
24
+ script.async = true;
25
+ script.id = 'memori-prism-script';
26
+
27
+ const autoloaderScript = document.createElement('script');
28
+ autoloaderScript.src =
29
+ 'https://cdn.jsdelivr.net/npm/prismjs@v1.29.0/plugins/autoloader/prism-autoloader.min.js';
30
+ autoloaderScript.async = true;
31
+ autoloaderScript.id = 'memori-prism-autoloader-script';
32
+
33
+ const prismCss = document.createElement('link');
34
+ prismCss.rel = 'stylesheet';
35
+ prismCss.href =
36
+ 'https://cdn.jsdelivr.net/npm/prismjs@1.29.0/themes/prism-tomorrow.min.css';
37
+
38
+ document.head.appendChild(prismCss);
39
+
40
+ document.head.appendChild(script);
41
+ document.head.appendChild(autoloaderScript);
42
+ };
37
43
 
38
44
  const Snippet = ({
39
45
  medium,
40
46
  className,
41
47
  preview = false,
42
- showLineNumbers = true,
43
48
  showCopyButton = true,
44
49
  }: Props) => {
45
50
  const { t } = useTranslation();
46
51
 
52
+ useEffect(() => {
53
+ loadPrismScripts();
54
+ }, []);
55
+
56
+ useEffect(() => {
57
+ // @ts-ignore
58
+ // eslint-disable-next-line no-undef
59
+ if ('Prism' in window && window.Prism.highlightAll) Prism.highlightAll();
60
+ }, [medium.content]);
61
+
47
62
  return (
48
63
  <div className="memori-snippet">
49
64
  <div className="memori-snippet--content">
50
- <SyntaxHighlighter
51
- aria-labelledby={`#snippet-${medium.mediumID}`}
52
- className={className}
53
- style={atomDark}
54
- showLineNumbers={showLineNumbers}
55
- language={
56
- prismSyntaxLangs.find(l => medium.mimeType === l.mimeType)?.lang ??
57
- 'text'
65
+ <pre
66
+ className={cx('line-numbers', className)}
67
+ aria-labelledby={
68
+ !!medium.title?.length ? `#snippet-${medium.mediumID}` : undefined
58
69
  }
59
70
  >
60
- {medium.content?.length && medium.content.length > 200 && preview
61
- ? `${medium.content.slice(0, 200)}\n...`
62
- : `${medium.content}`}
63
- </SyntaxHighlighter>
71
+ <code
72
+ className={`language-${
73
+ prismSyntaxLangs.find(l => medium.mimeType === l.mimeType)
74
+ ?.lang ?? 'text'
75
+ }`}
76
+ >
77
+ {medium.content?.length && medium.content.length > 200 && preview
78
+ ? `${medium.content.slice(0, 200)}\n...`
79
+ : `${medium.content}`}
80
+ </code>
81
+ </pre>
82
+
64
83
  {showCopyButton && (
65
84
  <Button
66
85
  padded={false}