werich

package module
v0.0.0-...-75a6659 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2019 License: MIT Imports: 7 Imported by: 0

README

Markdown 转微信小程序 rich-text

在小程序中展示富文本有多种选择:

  1. 后端提供 HTML 或 Markdown 文本,小程序端用 WxParse 渲染。
  2. 后端直接按照微信小程序 rich-text 组件的要求,提供数据。本仓库就是实现了这种方式。

优点:后端控制输出内容,在输出前可以对 Markdown 进行预处理。
缺点:网络传输内容变大,rich-text 组件不支持 <a> 标签。

使用说明

用 Markdown 直接生成微信小程序富文本,传给前端后前端赋值到 <rich-text> 的 nodes 即可。

示例:

package main

import (
    "fmt"
    "io/ioutil"

    "github.com/hyacinthus/werich"
)

func main() {
    input, err := ioutil.ReadFile("test.md")
    if err != nil {
        panic(err)
    }
    md := werich.NewMD(input)
    // rich
    rich := md.Rich()
    fmt.Println(string(rich))
}

自定义 CSS

生成的富文本节点 css 属性为 md_xxx ,我们提供了一份样例,复制到小程序在相应页面 导入即可。你可以修改其中的样式。如果对 class 名称不满意,我们也提供了添加前缀的 功能,请参考 werich.go 文件,自行使用 renderer 和 blackfriday 解析即可。

解析 Front matter

有很多写作软件和静态网站生成器在 Markdown 头部添加了元数据,我们支持解析 YAML 型 的元数据,别的类型您可以参考 werich.go 文件,自行编写。

package main

import (
    "fmt"
    "io/ioutil"

    "github.com/hyacinthus/werich"
)

func main() {
    input, err := ioutil.ReadFile("test.md")
    if err != nil {
        panic(err)
    }
    md := werich.NewMD(input)
    // meta
    var meta = make(map[string]interface{})
    err = md.Meta(meta)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%v",meta)
}

Markdown 语法限制

为了能在小程序和网页完美显示,我们对 Markdown 规则做了如下限制:

  • 链接在小程序中将只保留描述文字,在网页中正常,尽量避免使用链接。
  • 图片只能使用我们域名下的链接或者 "./xxx" 的形式,直接拖到编辑器就会是后者的效 果。
  • 图片不支持 title 语法,不支持 alt 语法,就光写地址就好了。
  • 不支持直接使用 html 语法
  • 列表最多只支持两层
  • 还没有支持代码高亮功能,最好不要在文章中包含代码。

Documentation

Overview

Package werich is a Blackfriday Markdown Processor WeApp rich-text Randerer Available at http://github.com/hyacinthus/werich

Copyright © 2018 Muninn <hyacinthus@gmail.com>. Distributed MIT License. See README.md for details.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MD

type MD struct {
	// contains filtered or unexported fields
}

MD Markdown entity

func NewMD

func NewMD(src []byte) *MD

NewMD make a MD

func (*MD) HTML

func (md *MD) HTML() []byte

HTML convert md to html

func (*MD) HasMeta

func (md *MD) HasMeta() bool

HasMeta 是否存在 meta data

func (*MD) Meta

func (md *MD) Meta(v interface{}) error

Meta unmarshal meta to struct or map v

func (*MD) Reader

func (md *MD) Reader() io.Reader

Reader 给需要的函数提供全文 Reader

func (*MD) Rich

func (md *MD) Rich() []byte

Rich render markdown to weapp rich-text json struct

func (*MD) SetMeta

func (md *MD) SetMeta(v interface{}) error

SetMeta 重新生成 meta 信息

func (*MD) Unix

func (md *MD) Unix()

Unix 将正文的换行符统一成unix样式 blackfriday 现在有个bug,无法完美支持 Windows 换行符

type Renderer

type Renderer struct {
	// If you define this as "pre-", the tag class will seems like "pre-md-xxx",
	// don't forget "-". The default is empty, clsss will be "md-xxx".
	CSSPrefix string
	// Heading level offset, if it is 2, <h2> will change to <h4>
	HeadingOffset int
}

Renderer is a type that implements the Renderer interface for rich-text output.

func (*Renderer) RenderFooter

func (r *Renderer) RenderFooter(w io.Writer, ast *bf.Node)

RenderFooter writes rich text nodes latter ]

func (*Renderer) RenderHeader

func (r *Renderer) RenderHeader(w io.Writer, ast *bf.Node)

RenderHeader writes rich text nodes front [

func (*Renderer) RenderNode

func (r *Renderer) RenderNode(w io.Writer, node *bf.Node, entering bool) bf.WalkStatus

RenderNode is a renderer of a single node of a markdown syntax tree. For block nodes it will be called twice: first time with entering=true, second time with entering=false, so that it could know when it's working on an open tag and when on close. It writes the result to w.

The return value is a way to tell the calling walker to adjust its walk pattern: e.g. it can terminate the traversal by returning Terminate. Or it can ask the walker to skip a subtree of this node by returning SkipChildren. The typical behavior is to return GoToNext, which asks for the usual traversal to the next node.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL