18 基于.NetCore开发博客项目 StarBlog - 实现本地Typora文章打包上传( 二 )


  • 基于.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
【18 基于.NetCore开发博客项目 StarBlog -实现本地Typora文章打包上传】

经验总结扩展阅读