0.1.1 • Published 6 years ago

i18next-react-markdown v0.1.1

Weekly downloads
109
License
MIT
Repository
github
Last release
6 years ago

i18next-react-markdown

Embed React elements using markdown in i18next translation strings.

This wraps marksy with an HTML element and React component config import util.

Getting Started

Install and Build

npm install

Storybook

Run local storybook

npm start

Build storybook

npm run storybook:build

outputs static storybook site to docs/

Usage

This can be used in your own project and elements can be overriden with custom React components.

Locale keys must have the suffix: _md to use markdown.

Locale usage

Example locale JSON

{
  "website": "Website",
  "website-header_md": "Visit $t(website)"
}

returns "Visit Website"

i18next processor initialisation

import mdProcessor, { parser as mdParser } from 'i18next-react-markdown';

const elements = {
  h1({ id, children }: Attributes) {
    return (
      <h1 id={id}>
        {children}
      </h1>
    );
  },
}

const components = {
  Card({ children }: Attributes) {
    return (
      <div class="card">
        {children}
      </div>
    );
  },
}

const mdProcessor = createProcessor({
  elements,
  components,
  marksyOptions: {} // additional marksy input after elements and components
  markedOptions: {}
});

i18n
  .use(mdProcessor)
  .init({
    // ...
    postProcess: ['react-markdown'],
    // ...
  })

Editor usage

import Editor from 'i18next-react-markdown/Editor';

return (
  <Editor elements={elements} components={components} />
)

Markdown parser usage

import { parser as createMdParser } from 'i18next-react-markdown';


const mdParse = createMdParser(elements, components);