summaryrefslogtreecommitdiffstats
path: root/src/assets/images/icon-articles.svg
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2022-01-29 22:59:50 +0100
committerArmand Philippot <git@armandphilippot.com>2022-01-29 22:59:50 +0100
commit4d858de0eae41b68af0e631629d0fc1015d16803 (patch)
tree3fe97c9f4ea04197865eadc1ff4317e89f3e3beb /src/assets/images/icon-articles.svg
parentce21fc2eb82af99d3f49b99f2e585d5073ef6bb6 (diff)
chore: translate in French
Diffstat (limited to 'src/assets/images/icon-articles.svg')
0 files changed, 0 insertions, 0 deletions
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
import Prism from 'prismjs';
import { useEffect, useMemo } from 'react';
import { useIntl } from 'react-intl';

const PRISM_PLUGINS = [
  'autoloader',
  'color-scheme',
  'command-line',
  'copy-to-clipboard',
  'diff-highlight',
  'inline-color',
  'line-highlight',
  'line-numbers',
  'match-braces',
  'normalize-whitespace',
  'show-language',
  'toolbar',
] as const;

export type PrismPlugin = typeof PRISM_PLUGINS[number];

export type DefaultPrismPlugin = Extract<
  PrismPlugin,
  | 'autoloader'
  | 'color-scheme'
  | 'copy-to-clipboard'
  | 'match-braces'
  | 'normalize-whitespace'
  | 'show-language'
  | 'toolbar'
>;

export type OptionalPrismPlugin = Exclude<PrismPlugin, DefaultPrismPlugin>;

export type PrismLanguage =
  | 'apacheconf'
  | 'bash'
  | 'css'
  | 'diff'
  | 'docker'
  | 'editorconfig'
  | 'ejs'
  | 'git'
  | 'graphql'
  | 'html'
  | 'ignore'
  | 'ini'
  | 'javascript'
  | 'jsdoc'
  | 'json'
  | 'jsx'
  | 'makefile'
  | 'markup'
  | 'php'
  | 'phpdoc'
  | 'regex'
  | 'scss'
  | 'shell-session'
  | 'smarty'
  | 'tcl'
  | 'toml'
  | 'tsx'
  | 'twig'
  | 'yaml';

export type PrismAttributes = {
  'data-prismjs-copy': string;
  'data-prismjs-copy-success': string;
  'data-prismjs-copy-error': string;
  'data-prismjs-color-scheme-dark': string;
  'data-prismjs-color-scheme-light': string;
};

export type UsePrismProps = {
  language?: PrismLanguage;
  plugins: OptionalPrismPlugin[];
};

export type UsePrismReturn = {
  attributes: PrismAttributes;
  className: string;
};

/**
 * Import and configure all given Prism plugins.
 *
 * @param {PrismPlugin[]} plugins - The Prism plugins to activate.
 */
const loadPrismPlugins = async (plugins: PrismPlugin[]) => {
  for (const plugin of plugins) {
    try {
      if (plugin === 'color-scheme') {
        await import(`@utils/plugins/prism-${plugin}`);
      } else {
        await import(`prismjs/plugins/${plugin}/prism-${plugin}.min.js`);
      }

      if (plugin === 'autoloader') {
        Prism.plugins.autoloader.languages_path = '/prism/';
      }
    } catch (error) {
      console.error('usePrism: an error occurred while loading Prism plugins.');
      console.error(error);
    }
  }
};

/**
 * Use Prism and its plugins.
 *
 * @param {UsePrismProps} props - An object of options.
 * @returns {UsePrismReturn} An object of data.
 */
const usePrism = ({ language, plugins }: UsePrismProps): UsePrismReturn => {
  /**
   * The order matter. Toolbar must be loaded before some other plugins.
   */
  const defaultPlugins: DefaultPrismPlugin[] = useMemo(
    () => [
      'toolbar',
      'autoloader',
      'show-language',
      'copy-to-clipboard',
      'color-scheme',
      'match-braces',
      'normalize-whitespace',
    ],
    []
  );

  useEffect(() => {
    loadPrismPlugins([...defaultPlugins, ...plugins]).then(() => {
      Prism.highlightAll();
    });
  }, [defaultPlugins, plugins]);

  const defaultClassName = 'match-braces';
  const languageClassName = language ? `language-${language}` : '';
  const pluginsClassName = plugins.join(' ');
  const className = `${defaultClassName} ${pluginsClassName} ${languageClassName}`;

  const intl = useIntl();
  const copyText = intl.formatMessage({
    defaultMessage: 'Copy',
    description: 'usePrism: copy button text (not clicked)',
    id: '6GySNl',
  });
  const copiedText = intl.formatMessage({
    defaultMessage: 'Copied!',
    description: 'usePrism: copy button text (clicked)',
    id: 'nsw6Th',
  });
  const errorText = intl.formatMessage({
    defaultMessage: 'Use Ctrl+c to copy',
    description: 'usePrism: copy button error text',
    id: 'lKhTGM',
  });
  const darkTheme = intl.formatMessage({
    defaultMessage: 'Dark Theme 🌙',
    description: 'usePrism: toggle dark theme button text',
    id: 'QLisK6',
  });
  const lightTheme = intl.formatMessage({
    defaultMessage: 'Light Theme 🌞',
    description: 'usePrism: toggle light theme button text',
    id: 'hHVgW3',
  });
  const attributes = {
    'data-prismjs-copy': copyText,
    'data-prismjs-copy-success': copiedText,
    'data-prismjs-copy-error': errorText,
    'data-prismjs-color-scheme-dark': darkTheme,
    'data-prismjs-color-scheme-light': lightTheme,
  };

  return {
    attributes,
    className,
  };
};

export default usePrism;