【问题标题】:Markdown text editor code, table, list not working properlyMarkdown 文本编辑器代码、表格、列表无法正常工作
【发布时间】:2021-05-27 02:58:29
【问题描述】:

我想用 Markdown 编辑器写我的博客文章,我的问题是当我用 Markdown 发布我的文章编辑时,代码块、列表项、表格等无法正常工作,另一部分是图像、链接等工作文件, 粗体字...

看起来像:

输出是:

我已经尝试过这样的代码:

posts.js文件

import fs from 'fs'
import path from 'path'
import matter from 'gray-matter'
import { sortByDate } from '@/utils/index'

const files = fs.readdirSync(path.join('posts'))

export function getPosts() {
  const posts = files.map((filename) => {
    const slug = filename.replace('.md', '')

    const markdownWithMeta = fs.readFileSync(
      path.join('posts', filename),
      'utf-8'
    )

    const { data: frontmatter } = matter(markdownWithMeta)

    return {
      slug,
      frontmatter,
    }
  })

  return posts.sort(sortByDate)
}

[slug.js]文件:

import fs from 'fs'
import path from 'path'
import matter from 'gray-matter'
import marked from 'marked'
import Link from 'next/link'
import Layout from '@/components/Layout'
import CategoryLabel from '@/components/CategoryLabel'

export default function PostPage({
  frontmatter: { title, category, date, cover_image, author, author_image },
  content,
  slug,
}) {
  return (
    <Layout title={title}>
      <Link href='/blog'>Go Back</Link>
      <div className='w-full px-10 py-6 bg-white rounded-lg shadow-md mt-6 md:grid-cols-6'>
        <div className='flex justify-between items-center mt-4'>
          <h1 className='text-5xl mb-7'>{title}</h1>
          <CategoryLabel>{category}</CategoryLabel>
        </div>
        <img src={cover_image} alt='' className='w-full rounded' />

        <div className='flex justify-between items-center bg-gray-100 p-2 my-8'>
          <div className='flex items-center'>
            <img
              src={author_image}
              alt=''
              className='mx-4 w-10 h-10 object-cover rounded-full hidden sm:block'
            />
            <h4>{author}</h4>
          </div>
          <div className='mr-4'>{date}</div>
        </div>

        <div className='blog-text mt-2'>
          <div dangerouslySetInnerHTML={{ __html: marked(content) }}></div>
        </div>
      </div>
    </Layout>
  )
}

export async function getStaticPaths() {
  const files = fs.readdirSync(path.join('posts'))

  const paths = files.map((filename) => ({
    params: {
      slug: filename.replace('.md', ''),
    },
  }))

  return {
    paths,
    fallback: false,
  }
}

export async function getStaticProps({ params: { slug } }) {
  const markdownWithMeta = fs.readFileSync(
    path.join('posts', slug + '.md'),
    'utf-8'
  )

  const { data: frontmatter, content } = matter(markdownWithMeta)
  return {
    props: {
      frontmatter,
      content,
      slug,
    },
  }
}

请给点建议。

【问题讨论】:

  • Tailwind CSS 本身默认隐藏列表装饰,你应该enable it explicitly.
  • @pan93412 请你告诉我应该在哪里改变。
  • 我不确定如何为marked 元素设置一个类。对不起:(

标签: javascript node.js reactjs next.js r-markdown


【解决方案1】:

见:https://nextjs.org/blog/markdown

您需要设置:https://github.com/tailwindlabs/tailwindcss-typography

然后将 prose 类添加到包含降价的 div 中

<div className='blog-text prose mt-2'>
      <div dangerouslySetInnerHTML={{ __html: marked(content) }}></div>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    • 2022-08-17
    相关资源
    最近更新 更多