JavaScript >> Javascript 文檔 >  >> Node.js

用 Node.js 編寫的 LeanPub 編譯和清理工具

LeanPub 是一個很棒的出版平台,但我們經常需要與參與圖書製作的其他團隊成員溝通,例如內容和文案編輯。在這種情況下,我知道 Guy Kawasaki 是對的——當我們在他的書 APE:作者、出版商、企業家中建議時——作者必須 使用 MS Word!

問題在於 LeanPub 使用多文件書籍格式和特殊的降價標記(這會使標記的應用感到困惑)。我正在完成一個新版本的 Rapid Prototyping with JS 書,之前將 txt 文件交給編輯的經驗很差。幸運的是,Node.js 提供了幫助!通過一些嘗試和錯誤嘗試,我發現這個工作流程最適合將 LeanPub 手稿轉換為一個 MS Word 文件(用於編輯和其他需要):

  1. 使用 Node.js 運行 leanpub-tool.js (node leanpub-tool.js)
  2. 在 Marked 應用中打開文件並轉換為 HTML
  3. 在 MS Word 中打開 HTML 並開始使用。

也可以在 https://gist.github.com/azat-co/5674684 上找到leanpub-tool.js 的完整代碼:


var fs = require('fs');
//change these to YOUR filenames
//probably we can read it from Book.txt but I was too lazy to implement it :-) and somebody might want to compile only sertain parts of the book
var book =[
"frontmatter.txt",
"mainmatter.txt",
"part1.txt",
"chapter1.txt",
"part2.txt",
"chapter2.txt",
"backmatter.txt",
"acknowledgment.txt"
];
 
var sanitizeText = [
  '{frontmatter}',
  '{backmatter}',
  '{mainmatter}',
  "I>## Note",
  "T>## Tip",
  "W>## Warning",
  '{lang="javascript"}',
  '{:lang="javascript"}',
  '{lang="css"}',
  '{:lang="css"}',
  '{lang="json"}',
  '{lang="ruby"}',
  '{lang="php"}',
  '{lang="text"}',
  '{lang="bash"}',
  '{lang="html"}',
  "I>",
  "T>",
  "W>"
]
 
 
var str = '';
//read files
book.forEach(function(chapter){
  str +=  fs.readFileSync(chapter,'utf8');
})
//sanitize LeanPub specific Markdown tags
sanitizeText.forEach(function(text){
//  console.log(text)
  //this loops through while there is not matches
  while (new RegExp (text).test(str)) {
    str = str.replace(text,'','gm')
  };
})
 
//write output to a file
fs.writeFileSync('leanpub-tool.txt',str);

Tutorial JavaScript 教程
  1. 改善網站設計的 10 個技巧。

  2. Shopify App From Scratch #8 - React Sidestep 2

  3. JavaScript 電池 API

  4. JavaScript 中的無點陷阱

  5. 介紹 AdonisJS

  6. 如何在 JavaScript 中將整數轉換為二進制?

  7. 未捕獲的 ReferenceError:未定義 Firebase

  1. 查找帶有邊框的 HTML5 Canvas(單擊)事件的坐標

  2. 在 React 上測試 HTML 事件

  3. 使用 Notion 作為你的數據庫

  4. JavaScript 中的 thenables

  5. 將 Material-UI 4 遷移到 Mui-5

  6. 如何使用 Next.js 創建一個基本的應用程序

  7. 如何防止在Javascript中雙擊選擇文本

  1. 使用 Jest 和 Enzyme 測試 React 中的組件

  2. 今天做了一些清潔,感覺想炫耀一下 :P;)

  3. 使用 Model-React 清潔 MVC 架構

  4. React 圖標:在 React 中使用圖標的最簡單方法