LoginSignup
8

More than 5 years have passed since last update.

NuxtでMarkdownエディタ(Qiitaみたいな)

Posted at

はじめに

NuxtでQiitaみたいな記事投稿ページを作りたいと思い、toast-ui.vue-editor
というnpmパッケージを使ったので、toast-ui.vue-editorの紹介:walking_tone1::walking:
markdown.mov.gif

準備

Nuxtのプロジェクト作成はnuxt/create-nuxt-app
を使用します。

以下をインストール:point_down:

npm install @toast-ui/vue-editor --save
npm install codemirror --save
npm install tui-editor --save
package.json
"dependencies": {
    "@toast-ui/vue-editor": "^1.0.2",
    "codemirror": "^5.42.0",
    "cross-env": "^5.2.0",
    "nuxt": "^2.0.0",
    "tui-editor": "^1.2.8",
    "vuetify": "^1.2.4"
  }

Challenge :santa:

まずはnuxt.config.jsでcssを読み込む。

nuxt.config.js
/*
 ** Global CSS
*/
  css: [
    '~/assets/style/app.styl',
    /* 以下を追加 ↓ */
    'tui-editor/dist/tui-editor.css',
    'tui-editor/dist/tui-editor-contents.css',
    'codemirror/lib/codemirror.css'
  ],

プレビューは previewStyleから"vertical”か"tab"を選択可
Markdownの表示だけならばViewerだけimportしてvalueにバインドさせれば表示できます。

<template>
  <div>
    <editor v-model="editorText" :options="editorOptions" :visible="editorVisible" height="500px" previewStyle="vertical" />
    <viewer :value="editorText" />
  </div>

</template>
<script>
  import {
    Editor,
    Viewer
  } from '@toast-ui/vue-editor'

  export default {
    components: {
      'editor': Editor,
      'viewer': Viewer
    },
    data() {
      return {
        editorText: '',
        editorOptions: {
          hideModeSwitch: true
        },
        editorVisible: true
      }
    }
  }
</script>

感想

toast-ui.vue-editorがいい!!:snowman2:
詳しいオプションは公式を見てください:point_right:nhnent/tui.editor

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8