Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Change automatic line breaks to optional #64

Merged
merged 7 commits into from
Jan 20, 2021
Merged

feat: Change automatic line breaks to optional #64

merged 7 commits into from
Jan 20, 2021

Conversation

akabekobeko
Copy link
Member

#50 対応。

@akabekobeko
Copy link
Member Author

今回の PR から API やプロパティーなどにコード ドキュメントを付けるようにしました。コードが自己言及的に書かれていればコメントは不要という思想もありますが、最もコードに近い場所へ意図などを記述する場としてコメントはあったほうがよいと考えているため、今後はなるべく書く予定です。

IDE や VS Code など入力補完の効く環境下で API リファレンスとして機能することも狙っています。

@akabekobeko
Copy link
Member Author

すみません。CLI 対応が抜けてたので改めてコミットしました。もし CLI を試される場合は sample.md を以下の内容で作成して

テスト
テスト

VFM を npm link した後に sample.md の置かれた場所で

$ vfm sample.md

$ vfm sample.md --auto-line-breaks

を比較してください。既定値 false なのでオプション未指定はそのまま、指定すると改行位置に <br> が挿入されます。

@akabekobeko
Copy link
Member Author

↑のコメントを書いて思ったのですが remark-breaks の処理は「変換」ではなく、改行位置へ <br> を挿入して元の改行は残るため「変換」以外で表現したほうがよいのかもしれません。

コード ドキュメントや CLI ヘルプでは "Convert" としましたが、なにかよい代替表現があればご指摘ください。またオプションもよりよい名前がありそうですね。

Copy link
Member

@spring-raining spring-raining left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PRありがとうございました。JSDocによるコメントなどありがたいです!

レビューコメント以外に一点、autoLineBreaks の有無による出力結果の変化についてテストがない状態なので、tests/block/ 以下に追加していただけるとありがたいです。
例えばGFMでは、改行1つによるautoLineBreaksはoffですが、行末に \ があると改行される仕様があります (https://gist.github.com/shaunlebron/746476e6e7a4d698b373 )。その点なども、testで確認できると良いかと思いました。


if (options.autoLineBreaks) {
// Line breaks are processed immediately after ruby
results.splice(3, 0, breaks);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indexをマジックナンバーで指定して挿入する実装は results の変更への追従を忘れてしまう恐れがあるためおすすめできません。同等の実装は以下のコードでも可能です。

  const results = [
    (snip)
    ...(options.autoLineBreaks ? [breaks] : []),
    (snip)
  ];

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spread operator で条件分岐いけるのですね。マジックナンバーよりも好ましいのでこの方法に変更します。

* @param options Options.
* @returns Parsers.
*/
export const reviveParse = (options: MarkdownOptions) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

返値の型として unified.PluggableList<unified.Settings> を明示していただけますか? その結果、results にある as unified.PluggableList<unified.Settings> を省略することが可能です。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

元コードで直に配列を返す際の as を踏襲していましたが、関数化したなら確かに返り値の型で明示するほうが好ましいですね。そのように変更します。

@akabekobeko
Copy link
Member Author

autoLineBreaks のテストですが、追加しようとしたら index.test.tshandle hard line break という名で存在していたためやめました。ただしこれは「常時 remark-breaks が実行されること」を想定して index.test.ts に定義されているようですので、

tests/block/ 以下に追加していただけるとありがたいです。

で対応します。

… option

If it is not expanded and is a normal argument, the caller must resolve the dependency between the properties. The VFM function avoids this by expansion, so referred to it.
1. Avoid `as` by explicitly specifying the return type of reviveParse
2. Independent test of line break handle
@akabekobeko
Copy link
Member Author

指摘のあった箇所を修正しました。reviveParse は返り値における型の明示ついでに処理が return のみとなったため、略記にしてあります。

@akabekobeko
Copy link
Member Author

現時点の対応でよろしければ merge しますので、お手すきの時に再レビューをお願いします。

@MurakamiShinyu
Copy link
Member

MurakamiShinyu commented Jan 19, 2021

--auto-line-breaks という名前ですが、長い行を自動で改行を入れるという意味かと誤解されるかもしれません。

Pandocはオプションで改行の扱いを変えることができるので、それに倣うのがよいと思います。

https://pandoc.org/MANUAL.html#extension-hard_line_breaks

Extension: hard_line_breaks

Causes all newlines within a paragraph to be interpreted as hard line breaks instead of spaces.

Extension: ignore_line_breaks

Causes newlines within a paragraph to be ignored, rather than being treated as spaces or as hard line breaks. This option is intended for use with East Asian languages where spaces are not used between words, but text is divided into lines for readability.

Extension: east_asian_line_breaks

Causes newlines within a paragraph to be ignored, rather than being treated as spaces or as hard line breaks, when they occur between two East Asian wide characters. This is a better choice than ignore_line_breaks for texts that include a mix of East Asian wide characters and other characters.

テスト:

$ pandoc --from=markdown+hard_line_breaks
The quick brown fox
jumps over the lazy dog.

<p>The quick brown fox<br />
jumps over the lazy dog.</p>

ということで、--auto-line-breaks--hard-line-breaks に(autoLineBreakshardLineBreaks に)変更することを提案します。

@akabekobeko
Copy link
Member Author

ということで、--auto-line-breaks--hard-line-breaks に(autoLineBreakshardLineBreaks に)変更することを提案します。

いい案ですね。対応します。

@akabekobeko
Copy link
Member Author

村上さんのオプション名案を反映しました

@akabekobeko
Copy link
Member Author

@spring-raining @MurakamiShinyu
現時点のコードでよろしければ、その旨をお知らせください。merge したいと思います。

README.md Outdated
@@ -24,6 +24,7 @@ Vivliostyle Flavored Markdown (VFM), a Markdown syntax optimized for book author
- [`partial` (default: `false`)](#partial-default-false)
- [`title` (default: `undefined`)](#title-default-undefined)
- [`language` (default: `undefined`)](#language-default-undefined)
- [`autoLineBreaks` (default: `false`)](#autoLineBreaks-default-false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- [`autoLineBreaks` (default: `false`)](#autoLineBreaks-default-false)
- [`hardLineBreaks` (default: `false`)](#hardLineBreaks-default-false)

README.md Outdated
@@ -210,6 +211,38 @@ will generates:
</html>
```

#### `autoLineBreaks` (default: `false`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#### `autoLineBreaks` (default: `false`)
#### `hardLineBreaks` (default: `false`)

README.md Outdated
new
line
`,
{ autoLineBreaks: true },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{ autoLineBreaks: true },
{ hardLineBreaks: true },

@akabekobeko
Copy link
Member Author

@spring-raining ありがとうございます。指摘のあった README の変更漏れを修正しました。

Copy link
Member

@spring-raining spring-raining left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

良さそうです!

@akabekobeko akabekobeko merged commit 922eb4b into master Jan 20, 2021
@akabekobeko akabekobeko deleted the #50 branch January 20, 2021 14:27
@akabekobeko
Copy link
Member Author

@spring-raining @MurakamiShinyu レビューありがとうございました!

Copy link
Member

@MurakamiShinyu MurakamiShinyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@akabekobeko akabekobeko added this to the v1.0.0 milestone Apr 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants