Skip to content

Instantly share code, notes, and snippets.

@takasek
Last active October 14, 2017 05:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takasek/9a2db34c310b3ad9480d5b9d4703395a to your computer and use it in GitHub Desktop.
Save takasek/9a2db34c310b3ad9480d5b9d4703395a to your computer and use it in GitHub Desktop.
WorkFlowyのトピックをMarkdown化するDeskFlowy( http://nap.jp/michi/DeskFlowy/ja/ )用スクリプト。1段目は ##、2段目は###、3段目は本文、4段目以降はリストとして扱います。
(function() {
var lastIndents = null;
var lastNode = null;
var lastParsedLine = null;
function stringify(node, indents) {
const text = node.children(".name").children(".content").text();
var lines = [];
function lastLineExists() {
if (lines.length > 0) {
// この関数内のパース結果を見る
return lines[lines.length - 1].length > 0;
}
// 前のパース結果を見る
if (lastParsedLine == null) {
// 最初のパース処理
return false;
}
return lastParsedLine.length > 0;
}
switch (indents) {
case 0: // h2見出し
if (lastLineExists()) {
lines.push("");
}
pre = "## ";
break;
case 1: // h3見出し
if (lastLineExists()) {
lines.push("");
}
pre = "### ";
break;
case 2: // 本文
// 本文始まりには空行
if (lastLineExists() && lastIndents != 2) {
lines.push("");
}
pre = "";
break;
default: // リスト
// リスト始まりか、前のノードがノートつきの場合には空行
if (lastLineExists() && (lastIndents < 3 || lastNode.hasClass("noted"))) {
lines.push("");
}
pre = "";
for (var i = 3; i < indents; i++) pre += " ";
pre += "- ";
break;
}
lines.push(pre + text);
if (node.hasClass("noted")) {
const notes = node.children(".notes").children(".content").text()
.replace(/\n$/, "")
.split("\n");
if (lastLineExists()) {
lines.push("");
}
if (notes[0].substring(0, 1) == ">") {
// > で始まるノートは引用
const fixedNotes = notes.map(function(n) {
return n.replace(/^>? ?/g, "> ")
});
lines = lines.concat(fixedNotes);
} else {
// > で始まらないノートはコードブロック等を想定
lines = lines.concat(notes);
}
}
return lines;
}
function parse(node, indents) {
var lines = [];
lines = lines.concat(stringify(node, indents).join("\n"));
lastIndents = indents;
lastNode = node;
lastParsedLine = lines[lines.length - 1];
const childNodes = node.children(".children").children(".project")
for (var i = 0; i < childNodes.length; i++) {
lines = lines.concat(parse($(childNodes[i]), indents + 1));
}
return lines;
}
const result = parse($(".project .open .selected"), 0);
alert(result.join("\n"));
})();
@michi10560
Copy link

michi10560 commented Sep 6, 2017

127c127
<                     lastLine = line;
---
>                     lastLine = lines;

@takasek
Copy link
Author

takasek commented Sep 7, 2017

ああっ、typoそのままにしてました、すみません。
前の行と比較して何かやりたい時のための変数なので、 lastLine = pre + c + post; のほうが正しいです。
…が、そもそも使われてないものなので削っちゃいますね><

@takasek
Copy link
Author

takasek commented Oct 5, 2017

WorkFlowyの仕様変更にともない処理を修正しました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment