golang 如何自定义解析 markdown to html ?

2023-05-16

golang 如何自定义解析 markdown to html ?

输出 HTML 模板内容如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Custom Markdown</title>
    <link rel="stylesheet" href="markdown.css"/>
</head>
<body>
{{ . }}
</body>
</html>

golang 程序如下

安装依赖 go get -u github.com/gomarkdown/markdown

package main

import (
	"bufio"
	"github.com/gomarkdown/markdown"
	"github.com/gomarkdown/markdown/ast"
	"github.com/gomarkdown/markdown/html"
	"html/template"
	"io"
	"log"
	"os"
)

// return (ast.GoToNext, true) to tell html renderer to skip rendering this node
// (because you've rendered it)
func renderHookCodeBlock(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {

	// skip all nodes that are not CodeBlock nodes
	if _, ok := node.(*ast.CodeBlock); !ok {
		return ast.GoToNext, false
	}

	// custom rendering logic for ast.CodeBlock. By doing nothing it won't be
	// 写入自定义数据
	w.Write([]byte(`<span>Hello World</span>`))

	return ast.GoToNext, false
}

func main() {

	inputFile := "markdown.md"           // 输入 md 文件
	inputTemplateFile := "template.html" // 模板文件
	outputFile := "markdown.html"        // 输出 html 文件

	opts := html.RendererOptions{
		Flags:          html.CommonFlags,
		RenderNodeHook: renderHookCodeBlock, // 设置自定义处理钩子
	}
	renderer := html.NewRenderer(opts)

	// 打开 md 文件
	fs, err := os.Open(inputFile)
	if err != nil {
		return
	}

	// 读取 md 内容
	var md string
	scanner := bufio.NewScanner(fs)
	for scanner.Scan() {
		md += scanner.Text() + "\n"
	}
	if scanner.Err() != nil {
		log.Panicln(err)
	}

	// md to html
	html := markdown.ToHTML([]byte(md), nil, renderer)

	tp, err := template.ParseFiles(inputTemplateFile)
	if err != nil {
		return
	}

	// 写入输出文件
	f, err := os.Create(outputFile)
	if err != nil {
		return
	}
	tp.Execute(f, template.HTML(html))

	print(string(html))
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

golang 如何自定义解析 markdown to html ? 的相关文章

  • proto_cmake_test

    proto cmake test Proto与CMAKE结合编译源代码 工程编译 span class token function cd span build cmake span class token punctuation span
  • Opencv获取指定时间内的视频片段以及帧

    文章目录 源码编译运行 源码 span class token comment gt File Name ddd cpp gt Author gt Mail 1 64 163 com gt Created Time 2022年06月17日
  • Opencv将目录下的图片存储为视频

    文章目录 源码编译运行 源码 span class token comment gt File Name main cpp gt Author gt Mail 1 64 163 com gt Created Time 2022年06月17日
  • 机器学习之svm---车牌识别

    目标 团队 承接嵌入式linux软硬件开发 机器视觉 图像处理 网络流等项目 微信号 xff1a hgz1173136060本文档尝试解答如下问题 如何使用OpenCV函数 CvSVM train 训练一个SVM分类器 xff0c 以及用
  • I2C调试工具

    1 I2C调试工具 i2c tools工具是开源I2C调试工具 xff0c 具有获取I2C总线挂载的设备列表及设备地址 xff0c 可对指定设备指定寄存器进行读写的功能 ubuntu安装 xff1a apt get install libi
  • Ubuntu18版本安装ROS

    最近不小心把虚拟机里的ROS弄坏了 xff0c 导致Linux都无法使用 也忘了快照导致所以软件重新安装 xff0c 在这里给大家分享一下ubuntu18版本如何安装ros以及我安装中出现的问题如何进行解决 注 xff1a 不同的ubunt
  • ZED相机快速使用指南

    1 安装SDK ZED SDK 3 8 Download Stereolabs 2 安装ros GitHub stereolabs zed ros wrapper ROS wrapper for the ZED SDK 其他教程 xff1a
  • vscode:前进后退快捷键

    1 xff09 后退 xff1a alt 43 2 xff09 前进 xff1a alt 43
  • git: tag 和 branch 的区别

    前言 tag 是什么 tag 翻译过来是标签的意思 xff0c 顾名思义 xff0c 标签是为了标记某种事物 tag 是 Git 版本库的一个快照 xff0c 指向某个 commit 的指针 tag 的好处 tag 的存在 xff0c 是因
  • QApplication a(argc,argv);崩溃

    Microsoft Visual C 43 43 Debug Library Debug Error Program de mytoolkit mytoolkit mytoolkit Win32 Debug mytoolkit exe Mo
  • jetson Xavier nx安装torch和torchvision,并解决解决版本不匹配(报错RuntimeError: Couldn‘t load custom C++ ops)的问题

    目录 1 安装torch 2 安装torchvision 3 验证是否安装成功 4 错误记录 5 torch和torchvision网盘链接 首先 xff0c torch和torchvision都不能直接pip安装 xff0c 以下的演示是
  • C++ 数据结构:DS顺序表--合并操作

    题目描述 建立顺序表的类 xff0c 属性包括 xff1a 数组 实际长度 最大长度 xff08 设定为1000 xff09 已知两个递增序列 xff0c 把两个序列的数据合并到顺序表中 xff0c 并使得顺序表的数据递增有序 输入 第1行
  • ubuntu安装多个版本python

    背景 xff1a 本地环境 Ubuntu 22 04 64 bit xff0c 默认安装python3 10 6 xff0c 未安装pip venv 需求 xff1a 安装python3 8 xff0c 并安装两版本对应pip xff0c
  • socket编程中recv()和read()的使用与区别

    recv和read相似 xff0c 都可用来接收sockfd发送的数据 xff0c 但recv比read多了一个参数 xff0c 也就是第四个参数 xff0c 它可以指定标志来控制如何接收数据 1 recv 原型 xff1a ssize t
  • stm32printf函数的串口输出代码

    stm32f103串口一与串口二printf函数输出 因项目需要特意配置了该段代码 xff0c 不喜勿喷 xff0c 纯属个人笔记 对于串口的代码网上也是很多 xff0c 无非是配置问题 xff0c 该代码是基于stm32f103c8t6来
  • C/C++ —— 小端转大端函数的使用

    函数说明 uint32 t htonl uint32 t hostlong uint16 t htons uint16 t hostshort uint32 t ntohl uint32 t netlong uint16 t ntohs u
  • AntDesign Upload组件上传图片

    技术选型 前端技术选型 xff1a React Hook 43 typescript antd版本 xff1a 3 18 使用Upload上传图片 上传效果截图 预览效果截图 项目中完整写法 xff1a span class token k
  • [LeetCode] 876. Middle of the Linked List

    Given a non empty singly linked list with head node head return a middle node of linked list If there are two middle nod
  • 博客搬家啦

    博客搬家啦 xff0c 新博客地址 https blueleek blue 小伙伴一起嗨学一起来
  • MFC 改变编辑框字体(大小,字体)

    1 在 Dlg h文件中加入 CFont font 字体对象 2 定义一个CEdit控件变量 CEdit m EditAa 61 CEdit GetDlgItem IDC A EDIT A 关联控件A CEdit m EditBb 61 C

随机推荐

  • STM32 中浮点转成字符串,解决 -u_printf_float

    开发偶遇问题 在C 43 43 或其他高级语言中 xff0c 以及C语言在Visual Studio或者linux平台上 xff0c 是可以直接通过sprintf将 xff0c int float转换成string类型的 xff1b 但是
  • 路由器原理(一)

    觉得之前对路由器的了解还不是特别清楚 xff0c 继续来学习 xff0c 是参考了路由器的原理及其配置 xff08 一 xff09 一 xff1a 路由器的基础知识 xff08 1 xff09 路由器的概述 xff1a 作为网络层的网络互连
  • 表单提交及后台数据接收

    用POST请求 xff0c 后台原生接收的一个公式 xff1a span class hljs transposed variable req span addListener span class hljs string 34 data
  • JSON怎样添加注释

    今天在写一个程序的时候发现了一个问题 xff0c 在json文件中添加注释之后 xff0c 程序就出现bug了 于是 xff0c 去搜了一下这个问题的相关解释 xff0c 在这里和大家分享一下 xff1a JSON为什么不能添加注释 xff
  • Converting circular structure to JSON

    在做手机验证码模块 xff0c 遇到一个bug Converting circular span class hljs keyword structure span span class hljs keyword to span JSON
  • Mac root和普通用户切换

    今日 xff0c 老身在用su 命令 xff0c 输入密码之后准备切换至root用户 但不幸得到这个消息 xff1a su Sorry 找到解决方案如下 xff1a span class hljs built in sudo span su
  • TCP UDP 的区别和具体应用场景

    TCP和UDP 两者都是通信协议 xff0c TCP和UDP都是传输层协议 xff0c 但是他们的通信机制和应用场景不同 TCP TCP xff08 Transmission Control Protocol xff09 又叫传输控制协议
  • 浏览器缓存机制详解(一)

    浏览器缓存机制可以极大的提升用户体验 xff0c 另一方面会因为读取缓存而展示了错误的东西 xff0c 因而在开发中要设法将其删除 什么是浏览器缓存 xff1f 浏览器缓存就是把一个已经请求过的web资源 xff08 如html页面 xff
  • G2O库和Eigen库can not find 问题——FindG2O.cmake FindEigen3.cmake

    目录 CMakeLists设置FindG2O cmake文件内容FindEigen3 cmake 文件内容 CMakeLists设置 在CMakeLists里加入如下这句话 xff0c 并在项目文件夹中新建cmake文件夹 LIST APP
  • sockaddr_in结构体

    点击进入结构如下 xff1a struct sockaddr in uint8 t sin len sa family t sin family in port t sin port struct in addr sin addr char
  • Win7下VS2010使用STLPort .

    Win7下VS2010使用STLPort 更新于2012年8月24日 星期五 http blog csdn net shunqiziranhao007 article details 7905435 STLport的下载地址 http so
  • 【极简笔记】OpenPose

    极简笔记 Realtime Multi Person 2D Pose Estimation using Part Affinity Fields 论文地址 xff1a https arxiv org abs 1611 08050 文章的核心
  • C++各类库

    1 C 43 43 各大有名库的介绍 C 43 43 标准库 2 C 43 43 各大有名库的介绍 准标准库Boost 3 C 43 43 各大有名库的介绍 GUI 4 C 43 43 各大有名库的介绍 网络通信 5 C 43 43 各大有
  • watch 50 行代码实现 windows watch

    watch 50 行代码实现 windows watch 最近想监控 gpu 使用情况 xff0c 无奈在 windows 上没有 watch 指令 xff0c nvidia smi lms 20 自带输出也不美观 xff0c 那么为何不自
  • intel CPU 命名含义

    https www intel com content www us en processors processor numbers html
  • 使用 c++ 如何一行行的读取文本数据?

    使用 std getline 函数读取文件行 https en cppreference com w cpp string basic string getline 下面进行不同写法读取文件行的方式的比较 for std string li
  • android NDK 如何使用 JNI 与 Java&Kotlin 语言交互?

    android NDK 如何使用 JNI 与 Java amp Kotlin 语言交互 xff1f http web archive org web 20120626135526 http java sun com docs books j
  • 海康威视摄像头 rtsp 地址格式

    海康威视摄像头 rtsp 地址格式 单播取流格式 rtsp username password 64 ip port Steaming Channels number 多播取流格式 rtsp username password 64 ip
  • debian 11.3 国内最快镜像源

    debian 11 3 国内最快镜像源 deb https mirrors tencent com debian bullseye main non free contrib deb src https mirrors tencent co
  • golang 如何自定义解析 markdown to html ?

    golang 如何自定义解析 markdown to html xff1f 输出 HTML 模板内容如下 span class token doctype span class token punctuation lt span span