Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Markdown を拡張する話

Markdown を拡張する話

at Markdown Night 2017 Summer

3aebd86547bfc5cdb451d5f2f95ed5d8?s=128

Koutarou Chikuba

August 29, 2017
Tweet

Transcript

  1. MarkdownΛ֦ு͢Δ࿩ mizchi @ Markdown Night 2017 Summer

  2. ࣗݾ঺հ • @mizchi • SPA৬ਓ/Node.jsΤϯδχΞ • 4݄͔ΒϑϦʔϥϯε

  3. ࠓ݄ͷਐḿ

  4. ࣗ෼ͱMarkdown • લʑ৬(Quipper)ͰMarkdownΛ༻͍ͨڭࡐΦʔαϦϯάπʔ ϧͷ։ൃ • લ৬(Qiita)Ͱ Kobito ༻ͷ Markdown ίϯύΠϥͷ։ൃ

    ۀ຿MarkdownΦʔαϦϯάྺɺܭ3೥͙Β͍
  5. ୭޲͚ͷൃද͔ • ԿΒ͔ͷࣄ৘Ͱ markdown Λ֦ு͠ͳ͚Ε͹ͳΒͳ͍ਓ • AST ͕޷͖ͳਓ • GFM/Commonmark

    ͷ࢓༷ΛकΔ੓࣏తਖ਼͠͞ͱ͔ɺ͜ͷൃද ͷதʹ͓͍ͯ͸Ͳ͏Ͱ΋͍͍…
  6. ࠓ೔ͷൃද಺༰ • ֦ுͷࢦ਑ • remark ΤίγεςϜͷ঺հ • ΑΓΑ͍ WYSIWYG ͷͨΊʹ

  7. ࠓ೔࿩͞ͳ͍͜ͱ • GFM/Commonmark ͳͲͷྺ࢙ɾৄࡉ • ଞͷൃදऀ͕ޠͬͯ͘ΕΔͰ͠ΐ͏ • ੲॻ͍ͨࢿྉ: QiitaͱMarkdownͱίϯςϯπΦʔαϦϯά http://qiita.com/mizchi/items/

    17bc6c123207be7e6636
  8. ֦ுͷࢦ਑Λߟ͑Δ

  9. Markdownͷྑ͍఺ • ϓϨʔϯςΩετͰ΋ ൺֱత ಡΈ΍͍͢ • ҟͳΔαʔϏεͰ૬ޓӡ༻ੑ͕͋Δ (GitHub, Qiita, ͸ͯ

    ͳϒϩά, etc…)
  10. Markdownͷྑ͍఺ • ϓϨʔϯςΩετͰ΋ ൺֱత ಡΈ΍͍͢ • ҟͳΔαʔϏεͰ૬ޓӡ༻ੑ͕͋Δ (GitHub, Qiita, ͸ͯ

    ͳϒϩά, etc…) <= ???
  11. ݱ࣮ • ͋ΔίϯύΠϥͷग़ྗͷɺผͷίϯύΠϥͰͷ࠶ݱ͸ࠔ೉ • https://babelmark.github.io/ • Markdown ͱ͍͏ʮجຊతͳ֓೦ʯΛڞ༗͢Δ͚ͩͷɺผͳԿ ͔ͩͱࢥ͏ͷ͕ແ೉

  12. Τοδέʔεͷྫ - a - b - c -d - e

    https://babelmark.github.io/?text=-+a++++-+b++-+c+ ++-d+-+e
  13. Markdownπʔϧ։ൃऀͱͯ͠ࢥ͏͜ͱ • ग़ྗϒϨΛग़͞ͳ͍ͨΊʹΫϥαόͰ୯Ұ࣮૷͕ྑ͍ • ΫϥΠΞϯτͰDOMΛԚ͞ͳ͍Α͏ʹ֦ு͍ͨ͠ • ϦΞϧλΠϜฤूͷͨΊʹதؒঢ়ଶ͔ΒVDOMΛు͖͍ͨ => ΑͬͯɺJS࣮૷͕Ұ൪خ͍͠ ※

    Node.js ΤϯδχΞͷҙݟ
  14. Ծ૝DOMϓϨϏϡʔͷར఺ • ૉ๿ͳ࣮૷͢Δͱɺଧ伴͝ͱʹ el.innerHTML = markdown.compile(md) ͕૸Δ • ංେԽ͢ΔͱೖྗͰக໋తʹ஗͘ͳΔ •

    AST·Ͱύʔεͯ͠Ծ૝DOMΞϧΰϦζϜʹͿΜ౤͛Δ͜ͱͰ 16ms ҎԼΛୡ੒Ͱ͖Δ
  15. Markdown֦ுͷࢦ਑ 1.֦ு͠ͳ͍ 2.֦ு͢ΔͳΒߦّΑ͘

  16. 1. ֦ு͠ͳ͍ • ҟͳΔαʔϏεؒͷ૬ޓӡ༻ੑΛഁյ͢ΔͱɺMarkdownͰ͋ ΔϝϦοτ͕ബΕΔ • ओཁͳϦςϥϧ(* _ - #

    + >) ͸ɺύʔεΛࠔ೉ʹ͢ΔͷͰ ৮Δ΂͖Ͱ͸ͳ͍
  17. 2. ֦ு͢ΔͳΒߦّΑ͘ • GFM ͷ fenced codeblock ͷத਎ΛͲ͏ग़ྗ͢Δ͔͸࣮࣭ ࣗ༝࿮ •

    ϦςϥϧΛ֦ு͢Δ৔߹ɺGFMͰ΋ະ࢓༷ͷϦςϥϧΛ࢖͏($ %) • جຊ͸ࣗ༝هड़ͱ͍͏ͷΛ๨Εͳ͍Α͏ʹ
  18. ྫ: fenced-codeblock ֦ு \`\`\`mermaid graph TD; A-->B; A-->C; B-->D; C-->D;

    \`\`\` mermaid.js ͷਤΛຒΊࠐΜͩΓ…
  19. ྫ: ਺ࣜϦςϥϧ֦ு $ x^2 + y^2 = z^2 $ •

    $ ~ $ Λ math ϊʔυͱͯ͠ύʔε • Khan/KaTeX ͰίϯύΠϧͯ͠ຒΊࠐΉ ͱ͔͍ͨ͠
  20. ࣮ࡍʹ͜ΕΒΛ࣮૷ͯ͠ΈΑ͏ʂ

  21. ͱࢥ͕ͬͨ… • લ৬ɺલʑ৬ͷϦϙδτϦʹίʔυ͸ஔ͍͖ͯͨ • ֓೦͚ͩஔ͍͍ͯ͘ͷͰ֤ࣗద౰ʹ࣮૷͍ͯͩ͘͠͞

  22. JS ͷ Markdown ॲཧܥ • chjj/marked • egoist/marked3 • showdown

    • markdown-js • wooorm/remark
  23. JS ͷ Markdown ॲཧܥ • chjj/marked • egoist/marked3 • showdown

    • markdown-js • wooorm/remark <- ͓͢͢Ί
  24. remark • github.com/wooorm/remark • wooorm ࢯ͸ࣗવݴޠॲཧͷݚڀऀɻ ଞɺ retext ͳͲ •

    ๛෋ͳϓϥάΠϯ: https://github.com/wooorm/ remark/blob/master/doc/plugins.md • GFMΦϓγϣϯɺ CommonmarkΦϓγϣϯ͕͋Δ
  25. remark: ຊମ • remark-parse: Markdown Parser • remark-stringify: MarkdownAST =>

    HTML • remark-cli: CLIπʔϧ
  26. remark: ϓϥάΠϯͷྫ • remark-react: MarkdownAST => React • remark-lint: Style

    Linter • remark-emoji: emoji֦ு • remark-lowlight: ίʔυͷγϯλοΫεϋΠϥΠτ • remark-inline-math: ਺֦ࣜு • remark-toc: ToCੜ੒
  27. remark: ΤίγεςϜ • unist: Universal AST • mdast: Markdown AST

    • hast: HTML AST ͦΕͧΕͷஈ֊͝ͱʹద༻Մೳͳ plugin ܊
  28. unist https://github.com/syntax-tree/unist • ൚༻తͳ AST ఆٛ • ࣮ମ͸ͳ͍͕ traverse ͷ࣮૷͕͋Δ

  29. mdast https://github.com/syntax-tree/mdast • Markdown ͷ AST ఆٛ • unist αϒηοτ

    • ࣮૷͸ https://github.com/wooorm/mdast-util-to- hast
  30. hast https://github.com/syntax-tree/hast • HTML ͷ AST ఆٛ • ͜ͷ૚ͷར఺ͱͯ͠ɺ hast

    => HTML ΍ hast => React ͷந৅͕Ͱ͖Δ • αχλΠβͳͲ΋͜ͷ૚Ͱ࣮૷
  31. remark: ࣮ࡍͷ֦ுͷྲྀΕ 1.remarkͷϓϥάΠϯͱ࣮ͯ͠૷ • ASTͷ෦෼ॻ͖׵͑ͳΒे෼ 2.ຊମΛ fork ࣮ͯ͠૷ • remark-parse

    ͷ tokenizer Λ࣮૷ • mdast-util-to-hast ͰରԠ͢Δϊʔυͷ HTML ग़ྗΛ هड़
  32. ৭ʑࢼͨ͠ྫ • https://github.com/mizchi/remark • https://github.com/mizchi/mdast-util-to-hast ※ϓϥάΠϯͷ࡞ྫ͕গͳ͍࣌୅ʹຊମΛforkͯ͠ ਺ࣜɺ emojiɺ@<username> Λ֦ுͨ͠ϦϙδτϦ

  33. remark: ·ͱΊ • ෳ਺ͷASTͷεςʔδʹհೖՄೳͳJSͷMarkdownίϯύΠϥ • ϓϥάΠϯ๛෋Ͱศར • ΈΜͳ΋ϓϥάΠϯΛ࡞Ζ͏!!!

  34. ΑΓΑ͍ WYSIWYG ͷઃܭͷͨΊʹ

  35. ͦ΋ͦ΋ WYSIWYG ͸ඞཁ͔ • ͿͬͪΌ͚ϓϩάϥϚಉ࢜ͳΒෆཁ • Markdown ͕ྲྀߦͬͨͷ͸ ෳࡶոحͳ WYSIWYG

    ΁ͷ൓ಈ • BtoC ͩͱආ͚ΒΕͳ͍ͷͰߟ͓͑ͯ͘
  36. Մٯʹม׵͢Ε͹…ʁ

  37. draft-js Ͱ markdown Λ૬ޓม׵ • draft-js: Facebook੡ͷWYSIWYGج൫ • draft-js-import-markdown Ͱ

    markdown Λೖྗ • draft-js-export-markdown Ͱ markdown Λग़ྗ
  38. draft-js Ͱ markdown Λ૬ޓม׵ • ಺෦දݱͷϚελʔσʔλΛmarkdownͰѻ͏ͱɺWYSIWYG <=> Markdown Λ૬ޓʹ੾Γସ͑ΒΕͯخ͍͠ͷͰ͸ •

    git Ͱ diff ΛऔΕΔ / PR ʹࡌͤΒΕΔ • Ͳͬͪͷਓ΋ຬ଍
  39. ͜ͷํ๏ͷ໰୊ • draft-js ͷ WYSIWYG ͷϓϨϏϡʔ࣮૷ͱ Markdownग़ྗ ͷʮݟͨ໨ʯΛ͋Θͤͳ͍ͱ͍͚ͳ͍ • A

    => B => A ͕Մٯ͔͸ผͷ໰୊ • draft-js ͷ࣮૷ͱ֦ு͕݁ߏਏ͍ (immutable.js ͷਂ͍ ཧղ͕ඞཁ) ͍ͣΕ΋ʮؾ߹ʯͰղܾ͢Δ͕…
  40. ଞͷҊ • slate.js: draft-js ͷ alternative • prosemirror: ·͞ʹ markdown

    <=> wysiwyg ͕֦ͩு ੑ͕௿͍ • markdown ͦͷ΋ͷʹϋΠϥΠτΛ͢Δ + ೖྗࢧԉ
  41. ݁࿦ ؤுΕ͹Ͱ͖Δ͕ؤுΓͨ͘ͳ͍

  42. ࠷ޙʹ • Markdown͸ߦّΑ֦͘ு͠·͠ΐ͏ • ༻్ʹԠͯ͡MarkdownίϯύΠϥΛ࡞Γ͍ͨํ͸ @mizchi ͕͓࢓ࣄঝΓ·͢

  43. ͓·͚1: ReactNativeͷՄೳੑ • mdast => react-native-elements ʹίϯύΠϧ͢Ε͹ɺ WebViewΛආ͚ͯߴ଎ͳϓϨϏϡʔ͕ՄೳͰ͸ • ΍Ε͹Ͱ͖Δ͜ͱ͸Θ͔ͬͯΔ͚Ͳ…

    • ࣌ؒͳͯ͘΍ͬͯͳ͍ • ͲͷυϝΠϯͷձࣾͰͦΕ͕ඞཁ͔ෆ໌
  44. ͓·͚2: Node.jsӡ༻ͷ໰୊ • SEOॏࢹͷ৔߹ɺ JS࣮૷ͩͱ React ͷ Server Side Rendering

    ͱಉ͡ɺͲ͜ͰNodeϓϩηεΛ؅ཧ͢Δ͔ͷ໰୊ ͕ൃੜ • NodeϓϩηεΛͿΒԼ͛Δ͔ɺ AWS Lambda ্ͷϚΠΫϩα ʔϏεʹͯ͠͠·͏͔
  45. ͓·͚3: γϯλοΫεϋΠϥΠλʔ͕Ͱ͔͍໰ ୊ • highlight.js, lowlight ͍ͣΕ΋ɺΫϥΠΞϯτ͕ίʔυ ϋΠϥΠλʔΛ࣋ͭͱɺߏจఆٛͰαΠζ͕๲ΕΔ • ඞཁͳίʔυ͚ͩϩʔυ͢Δ࢓૊ΈΛ࡞ΕΔΑ͏ʹ͢Δͷ͕೉

    ͍͠
  46. ͓·͚4: WASM • C++/Rust ͰMarkdownίϯύΠϥॻ͍ͯ WASM ΛΫϥΠΞϯ τʹ࣋ͬͯ͘Ε͹ཧ࿦্Ϣχόʔαϧ • ͦΕ͸ͦ͏͕ͩɺ·ͩք۾ͷ

    WASM ͷ஌ݟ͕଍Γͳ͍ʂʂʂ
  47. ͓·͚5: Kobito for Windows ͷ࣮૷ • codemirror ͷ markdown ϋΠϥΠτϞʔυ

    • codemirror ͷίʔυ͕෗ͬͯͯਏ͍ • γϯλοΫεϋΠϥΠλʔ͕Ͱ͔͍໰୊ΛҾ͖ͣͬͯͯ 1.5MB ͋Δ https://github.com/increments/kobito-oss/tree/ master/packages/markdown-editor
  48. ͓·͚6: md2react • ↑ kobito ͷҝʹ։ൃͨ͠ɻ • Reactͷ dangerouslySetInnerHTML ͸ɺ໊લͷ௨ΓյΕ

    Δ • ࠓ͸ remark-react ࢖͍ͬͯͩ͘͞
  49. Ҏ্