Git Product home page Git Product logo

unplugin-markdoc's Introduction

unplugin-markdoc

Markdoc plugin for Vite/Webpack projects.

Install

npm install @markdoc/markdoc unplugin-markdoc
Vite
// vite.config.ts
import MarkdocPlugin from 'unplugin-markdoc/vite'

export default defineConfig({
  plugins: [
    MarkdocPlugin({ /* options */ }),
  ],
})


Rollup
// rollup.config.js
import MarkdocPlugin from 'unplugin-markdoc/rollup'

export default {
  plugins: [
    MarkdocPlugin({ /* options */ }),
  ],
}


Webpack
// webpack.config.js
module.exports = {
  /* ... */
  plugins: [
    require('unplugin-markdoc/webpack')({ /* options */ })
  ]
}


Nuxt
// nuxt.config.js
export default {
  buildModules: [
    ['unplugin-markdoc/nuxt', { /* options */ }],
  ],
}

This module works for both Nuxt 2 and Nuxt Vite


Vue CLI
// vue.config.js
module.exports = {
  configureWebpack: {
    plugins: [
      require('unplugin-markdoc/webpack')({ /* options */ }),
    ],
  },
}


esbuild
// esbuild.config.js
import { build } from 'esbuild'
import MarkdocPlugin from 'unplugin-markdoc/esbuild'

build({
  plugins: [MarkdocPlugin()],
})


Usage

---
title: What is Markdoc?
---

# {% $markdoc.frontmatter.title %} {% #overview %}

Markdoc is a Markdown-based syntax and toolchain for creating custom documentation sites. Stripe created Markdoc to power [our public docs](http://stripe.com/docs).

{% callout type="check" %}
Markdoc is open-source—check out its [source](http://github.com/markdoc/markdoc) to see how it works.
{% /callout %}

Example with Vue

<script setup>
import Markdoc from '@markdoc/markdoc'
import content from './contents/doc.md'

const html = Markdoc.renderers.html(content)
</script>

<template>
  <div v-html="html" />
</template>

or to render a html directly...

<script setup>
import html from './contents/doc.md?html'
</script>

<template>
  <div v-html="html" />
</template>

Configuration

The plugin accepts an optional Markdoc.transform config:

// vite.config.ts
import MarkdocPlugin from 'unplugin-markdoc/vite'

export default defineConfig({
  plugins: [MarkdocPlugin({
    nodes: {},
    tags: {},
  })]
})

TypeScript Shim

declare module '*.md' {
  import type { RenderableTreeNode } from '@markdoc/markdoc'

  const Node: RenderableTreeNode
  export default Node
}

declare module '*.md?html' {
  const html: string
  export default html
}

Save as markdoc.d.ts for instance.

License

MIT

unplugin-markdoc's People

Contributors

wobsoriano avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

unplugin-markdoc's Issues

Dynamic require of "path" is not supported.

I created a project using vite's react-ts template and am going to use Markdoc.

I followed the README of markdoc and unplugin-markdoc and let me share details of configs.

vite.config.js

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import MarkdocPlugin from "unplugin-markdoc/vite";
import path from "path";

// https://vitejs.dev/config/
export default defineConfig({
	plugins: [react(), MarkdocPlugin()],
	resolve: {
		alias: {
			"@": path.resolve(__dirname, "./src"),
			"@containers": path.resolve(__dirname, "./src/containers"),
			"@pages": path.resolve(__dirname, "./src/pages"),
		},
	},
});

Home.tsx

import { Document } from "@containers";

export function Home() {
	return (
		<div>
			<Document
				source={`
					# Hello world.
					> My first Markdoc page
				`}
			/>
		</div>
	);
}

export default Home;

Document.tsx

import React from "react";
import Markdoc from "@markdoc/markdoc";

const config = {};

interface DocumentProps {
	source: string;
}

export function Document({ source }: DocumentProps) {
	const ast = Markdoc.parse(source);
	const content = Markdoc.transform(ast, config);

	return Markdoc.renderers.react(content, React);
}

export default Document;

And ran yarn dev but seeing the below error.

$ yarn dev
yarn run v1.22.19
$ vite
failed to load config from C:\repos\docs\vite.config.ts
error when starting dev server:
Error: Dynamic require of "path" is not supported
    at file:///C:/repos/docs/node_modules/unplugin-markdoc/dist/chunk-HWMXROXT.js:29:9
    at node_modules/.pnpm/[email protected]/node_modules/picomatch/lib/picomatch.js (file:///C:/repos/docs/node_modules/unplugin-markdoc/dist/chunk-HWMXROXT.js:1355:16)
    at __require2 (file:///C:/repos/docs/node_modules/unplugin-markdoc/dist/chunk-HWMXROXT.js:32:50)
    at node_modules/.pnpm/[email protected]/node_modules/picomatch/index.js (file:///C:/repos/docs/node_modules/unplugin-markdoc/dist/chunk-HWMXROXT.js:1499:22)        
    at __require2 (file:///C:/repos/docs/node_modules/unplugin-markdoc/dist/chunk-HWMXROXT.js:32:50)
    at file:///C:/repos/docs/node_modules/unplugin-markdoc/dist/chunk-HWMXROXT.js:1507:32
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.