xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

cnblogs 停更, 迁移到 GitHub SSR website All In One

cnblogs 停更, 迁移到 GitHub SSR website All In One

cnblogs 迁移,停更

old

👻 Say Good Bye!

https://www.cnblogs.com/xgqfrms/

new

🎉 Welcome to the freedom world!

https://cnblogs.xgqfrms.xyz/

https://github.com/xgqfrms/cnblogs

审核 💩

image

demos

export backup

https://i.cnblogs.com/posts/export

image

image

SQLite
JSON
XML(RSS)

"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2023-06-01
 * @modified
 *
 * @description
 * @augments
 * @example
 * @link https://www.cnblogs.com/xgqfrms/p/13983568.html
 * @link https://stackoverflow.com/questions/21194934/how-to-create-a-directory-if-it-doesnt-exist-using-node-js/71735771#71735771
 * @link https://coderrocketfuel.com/article/get-the-path-of-the-current-working-directory-in-node-js
 * @link
 * @link https://nodejs.dev/en/learn/writing-files-with-nodejs/
 * @link https://nodejs.org/docs/latest/api/fs.html
 * @link https://stackoverflow.com/questions/2496710/writing-to-files-in-node-js
 *
 */

// import fs from "node:fs/promises";
import * as pfs from "node:fs/promises";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from 'url';

// 手动解析 JSON 数据 ✅
import * as JSONObj from "./posts.json" assert {type: "json"};

const log = console.log;

const __filename = fileURLToPath(import.meta.url);
const dir = path.dirname(__filename);
log(`__filename =`, __filename)
log(`dir =`, dir)

const arr = JSONObj.default;
for (const obj of arr) {
  const {Title: title, Body: body} = obj;
  const folder = path.resolve(path.join(dir), `blogs`);
  // const folder = path.resolve(`${path.join(dir)}/blogs`);
  if (!fs.existsSync(folder)) {
    fs.mkdirSync(folder);
  }
  const filename = path.resolve(`${folder}/${title}.md`);
  fs.writeFile(filename, body, err => {
    if (err) {
      log(`❌`, err)
    } else {
      log(`✅ filename =`, filename)
    }
  })
}

async function test() {
  try {
    const filename = `${path.join(dir)}_promise.md`;
    await pfs.writeFile(filename, `promise version fs`)
    log(`✅ filename =`, filename)
  } catch (err) {
    log(`promise error ❌`, err)
  }
}

test();

image

https://github.com/xgqfrms/cnblogs-backup/blob/main/cnblogs-2023/json-to-markdown/app.mjs

https://www.cnblogs.com/xgqfrms/p/13983568.html

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

2023.09.02 更新

.html to .md

直接获取 markdown 文件,无需解析 JSON,手动生成数据了

// 

https://www.cnblogs.com/xgqfrms/p/17673122.html

https://www.cnblogs.com/xgqfrms/p/17673122.md

refs

https://www.cnblogs.com/xgqfrms/p/17121243.html

https://www.cnblogs.com/xgqfrms/p/13983568.html

https://stackoverflow.com/questions/21194934/how-to-create-a-directory-if-it-doesnt-exist-using-node-js/71735771#71735771

https://coderrocketfuel.com/article/get-the-path-of-the-current-working-directory-in-node-js

https://nodejs.dev/en/learn/writing-files-with-nodejs/

https://nodejs.org/docs/latest/api/fs.html

https://stackoverflow.com/questions/2496710/writing-to-files-in-node-js



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2023-06-05 11:41  xgqfrms  阅读(32)  评论(9编辑  收藏  举报