Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Index react example text in markdown, converted to React components.

License

NotificationsYou must be signed in to change notification settings

kktjs/markdown-react-code-preview-loader

 
 

Repository files navigation

CInpm versionnpm unpkg

Index example text in Markdown, converted to React components. The current package is theloader ofwebpack, which loads themarkdown document by configuring the currentloader, returning aJS object containing themarkdown text, the example index in themarkdown text.

Install Loader

npm i markdown-react-code-preview-loader -D

Configure Loader

After installing the dependency (loader), we need to configure theloader into thewebpack configuration. Learn how to use the configurationloader by using two configuration methods inkkt.

① The first method, use the mdCodeModulesLoader method

mdCodeModulesLoader method for addingmarkdown-react-code-preview-loader to webpack config.

// .kktrc.tsimportscopePluginOptionsfrom'@kkt/scope-plugin-options';import{LoaderConfOptions,WebpackConfiguration}from'kkt';import{mdCodeModulesLoader}from'markdown-react-code-preview-loader';exportdefault(conf:WebpackConfiguration,env:'development'|'production',options:LoaderConfOptions)=>{// ....conf=mdCodeModulesLoader(conf);// ....returnconf;};
importwebpackfrom'webpack';import{Options}from'markdown-react-code-preview-loader';/** * `mdCodeModulesLoader` method for adding * `markdown-react-code-preview-loader` to webpack config. * *@param {webpack.Configuration} config webpack config *@param {string[]} lang Parsing language *@param {Options} option Loader Options *@returns {webpack.Configuration} * **/exportdeclareconstmdCodeModulesLoader:(config:webpack.Configuration,lang?:string[],option?:Options)=>webpack.Configuration;

② The second method is to manually add the configuration

The configuration and usage methods are consistent in Webpack.

// .kktrc.tsimportwebpack,{Configuration}from'webpack';importscopePluginOptionsfrom'@kkt/scope-plugin-options';import{LoaderConfOptions}from'kkt';exportdefault(conf:Configuration,env:'development'|'production',options:LoaderConfOptions)=>{// ....config.module.rules.forEach((ruleItem)=>{if(typeofruleItem==='object'){if(ruleItem.oneOf){ruleItem.oneOf.unshift({test:/.md$/,use:[{loader:'markdown-react-code-preview-loader',options:{lang:["jsx","tsx"]},},],});}}});// ....returnconf;};

options parameter

import{PluginItem}from'@babel/core';import{OptionsasRemoveImportsOptions}from'babel-plugin-transform-remove-imports'exporttypeOptions={/**   * Language to parse code blocks, default: `["jsx","tsx"]`   */lang?:string[];/**   * Option settings for the babel (babel-plugin-transform-remove-imports) package   * https://github.com/uiwjs/babel-plugin-transform-remove-imports   */removeImports?:RemoveImportsOptions;/**   * Add babel plugins.   */babelPlugins?:PluginItem[];/**Do you want to parse the title*/isHeading?:boolean}

Used in the project

After addingloader, use the method to loadmarkdown text in the project project:

importmdObjfrom'markdown-react-code-preview-loader/README.md';// `README.md` raw string textmdObj.source// The component index object,// the React component converted from the markdown indexed example.// (need to configure meta)mdObj.components// The component source code index object, the sample source code indexed from markdown.// (need to configure meta)mdObj.data// This is the parsed header datamdObj.headings
{data:{77:{code:"\"use strict\";\n\nfunction ......"language:"jsx"name:77,meta:{},value:"impo....."},demo12:{code:"\"use strict\";\n\nfunction ......"language:"jsx"name:'demo12',meta:{},value:"impo....."}},components:{77:ƒ,demo12:ƒ},source:"# Alert 确认对话框....",headings:[{depth:1,value:"标题", ...},...]}
exporttypeCodeBlockItem={/** The code after the source code conversion. **/code?:string;/** original code block **/value?:string;/** code block programming language **/language?:string;/** The index name, which can be customized, can be a row number. */name?:string|number;/** The `meta` parameter is converted into an `object`. */meta?:Record<string,string>;};exporttypeCodeBlockData={source:string;components:Record<CodeBlockItem['name'],React.FC>;data:Record<CodeBlockItem['name'],CodeBlockItem>;headings?:HeadingItem[]};

isMeta

import{isMeta}from'markdown-react-code-preview-loader';isMeta('mdx:preview')// => trueisMeta('mdx:preview:demo12')// => trueisMeta('mdx:preview--demo12')// => false

getMetaId

import{getMetaId}from'markdown-react-code-preview-loader';getMetaId('mdx:preview')// => ''getMetaId('mdx:preview:demo12')// => 'demo12'

getURLParameters

import{getURLParameters}from'markdown-react-code-preview-loader';getURLParameters('name=Adam&surname=Smith')// => { name: 'Adam', surname: "Smith" }getURLParameters('mdx:preview:demo12')// => { }getURLParameters('mdx:preview:demo12&name=Adam&surname=Smith')// => { name: 'Adam', surname: "Smith" }getURLParameters('mdx:preview:demo12&code=true&boreder=0')// => { code: 'true', boreder: "0" }getURLParameters('mdx:preview:demo12?code=true&boreder=0')// => { code: 'true', boreder: "0" }
\```tsx mdx:preview:demo12&code=true&boreder=0import React from "react"const Demo = ()=>{  return <div>测试</div>}export default Demo\```
{  data:{    demo12:{      code:"\"use strict\";\n\nfunction ......"      language:"jsx"      name:'demo12',meta:{code:'true',boreder:'0'},value:"impo....."}},  components:{demo12:ƒ},source:"# Alert 确认对话框...."}

getCodeBlock

constgetCodeBlock:(child:MarkdownParseData['children'],opts?:Options)=>CodeBlockData['data'];

Configure meta ID

Note⚠️: You need to add a specialmeta identifier to the code block example, andloader will index thereact example for code conversion.

    Meta Tag         Meta ID   Meta Param    ┈┈┈┈┈┈┈┈         ┈┈┈┈┈┈┈   ┈┈┈┈┈┈┈┈┈┈╭┈┈┈┈┈┈┈┈━╲━━━━━━━━━━━━╱━━━━━━━╱━━━━━┈┈┈┈╮┆ ```jsx mdx:preview:demo12&boreder=0    ┆┆ import React from "react"              ┆┆ const Demo = () => <div>Test</div>     ┆┆ export default Demo                    ┆┆ ```                                    ┆╰┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈╯
  1. mdx: special identifier prefix
  2. mdx:preview Controls whether to perform example indexing, and obtain the required example object through the corresponding line number.
  3. mdx:preview:demo12 Uniquely identified bydemo12, accurately obtain theexample code orexample component object of the index.
  4. mdx:preview:&code=true&border=0 pass the parameters for the rendering layer to use.
\```tsx mdx:previewimport React from "react"const Demo = ()=>{  return <div>测试</div>}export default Demo\```
\```tsx mdx:preview:demo12import React from "react"const Demo = ()=>{  return <div>测试</div>}export default Demo\```
\```tsx mdx:preview:demo12&code=true&boreder=0import React from "react"const Demo = ()=>{  return <div>测试</div>}export default Demo\```

Development

npm install# Install dependenciesnpm install --workspaces# Install sub packages dependenciesnpm run watchnpm run start

Contributors

As always, thanks to our amazing contributors!

Made withaction-contributors.

License

Licensed under the MIT License.

About

Index react example text in markdown, converted to React components.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript89.9%
  • HTML6.1%
  • Less4.0%

[8]ページ先頭

©2009-2025 Movatter.jp