- 基于.NetCore开发博客项目 StarBlog - (4) markdown博客批量导入
- C#解析Markdown文档 , 实现替换图片链接操作
因为是做单篇文章导入 , 所以我这里获取临时目录写的所有
*.md
文件之后只取第一个文件来处理(理论上也不应该有多个~)var dir = new DirectoryInfo(extractPath);var files = dir.GetFiles("*.md");var mdFile = files.First();using var reader = mdFile.OpenText();var content = await reader.ReadToEndAsync();var post = new Post {Id = GuidUtils.GuidTo16String(),Status = "已发布",Title = dto.Title ?? $"{DateTime.Now.ToLongDateString()} 文章",IsPublish = true,Content = content,Path = "",CreationTime = DateTime.Now,LastUpdateTime = DateTime.Now,CategoryId = dto.CategoryId,};var assetsPath = Path.Combine(_environment.WebRootPath, "media", "blog");var processor = new PostProcessor(extractPath, assetsPath, post);// 处理文章标题和状态processor.InflateStatusTitle();// 处理文章正文内容// 导入文章的时候一并导入文章里的图片 , 并对图片相对路径做替换操作post.Content = processor.MarkdownParse();post.Summary = processor.GetSummary(200);
Markdown相关的处理 , 我封装了 PostProcessor
这个对象 , 在 StarBlog.Share
里 。处理多级分类如果文章的分类不是一级分类 , 那么把它上面的所有分类找出来 , 一个个排好队 , 方便后面处理 。
// 处理多级分类var category = await _categoryRepo.Where(a => a.Id == dto.CategoryId).FirstAsync();if (category == null) {post.Categories = "0";}else {var categories = new List<Category> {category};var parent = category.Parent;while (parent != null) {categories.Add(parent);parent = parent.Parent;}categories.Reverse();post.Categories = string.Join(",", categories.Select(a => a.Id));}
最后保存搞定~// 存入数据库post = await _postRepo.InsertAsync(post);
系列文章- 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客?
- 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目
- 基于.NetCore开发博客项目 StarBlog - (3) 模型设计
- 基于.NetCore开发博客项目 StarBlog - (4) markdown博客批量导入
- 基于.NetCore开发博客项目 StarBlog - (5) 开始搭建Web项目
- 基于.NetCore开发博客项目 StarBlog - (6) 页面开发之博客文章列表
- 基于.NetCore开发博客项目 StarBlog - (7) 页面开发之文章详情页面
- 基于.NetCore开发博客项目 StarBlog - (8) 分类层级结构展示
- 基于.NetCore开发博客项目 StarBlog - (9) 图片批量导入
- 基于.NetCore开发博客项目 StarBlog - (10) 图片瀑布流
- 基于.NetCore开发博客项目 StarBlog - (11) 实现访问统计
- 基于.NetCore开发博客项目 StarBlog - (12) Razor页面动态编译
- 基于.NetCore开发博客项目 StarBlog - (13) 加入友情链接功能
- 基于.NetCore开发博客项目 StarBlog - (14) 实现主题切换功能
- 基于.NetCore开发博客项目 StarBlog - (15) 生成随机尺寸图片
- 基于.NetCore开发博客项目 StarBlog - (16) 一些新功能 (监控/统计/配置/初始化)
- 基于.NetCore开发博客项目 StarBlog - (17) 自动下载文章里的外部图片
- 基于.NetCore开发博客项目 StarBlog - (18) 实现本地Typora文章打包上传
- https://www.cnblogs.com/liguix/p/11883248.html
经验总结扩展阅读
- 一 CPS攻击案例——基于脉冲宽度调制PWM的无人机攻击
- 房地产商没有预售许可证怎么回事
- Vue3 Vite3 多环境配置 - 基于 vite 创建 vue3 全家桶项目(续篇)
- 2 Libgdx游戏开发——接水滴游戏实现
- 五 Qt+ECharts开发笔记:ECharts的动态排序柱状图介绍、基础使用和Qt封装Demo
- 18-基于CentOS7搭建RabbitMQ3.10.7集群镜像队列+HaProxy+Keepalived高可用架构
- Asp-Net-Core开发笔记:集成Hangfire实现异步任务队列和定时任务
- 开发商为何会热衷于豪宅项目
- 驱动开发:通过Async反向与内核通信
- 云原生下基于K8S声明式GitOps持续部署工具ArgoCD实战-上