diff --git a/docs/vfm.md b/docs/vfm.md index 2715ae1..14527aa 100644 --- a/docs/vfm.md +++ b/docs/vfm.md @@ -12,6 +12,7 @@ Vivliostyle Flavored Markdown (VFM), a Markdown syntax optimized for book author - [Image](#image) - [with caption and single line](#with-caption-and-single-line) - [Ruby](#ruby) + - [Escape pipe in ruby body](#escape-pipe-in-ruby-body) - [Sectionization](#sectionization) - [Plain section](#plain-section) - [Raw HTML](#raw-html) @@ -169,8 +170,6 @@ figure figcaption { ## Ruby -PRE-RELEASE - **VFM** ``` @@ -192,6 +191,22 @@ ruby rt { } ``` +### Escape pipe in ruby body + +If want to escape the delimiter pipe `|`, add `\` immediately before it. + +**VFM** + +``` +{a\|b|c} +``` + +**HTML** + +```html +

a|bc

+``` + ## Sectionization Make the heading a hierarchical section. diff --git a/src/plugins/ruby.ts b/src/plugins/ruby.ts index 2777c62..b5fb73c 100644 --- a/src/plugins/ruby.ts +++ b/src/plugins/ruby.ts @@ -10,7 +10,8 @@ function locateRuby(value: string, fromIndex: number) { const tokenizer: Tokenizer = function (eat, value, silent) { const now = eat.now(); - const match = /^{(.+?)\|(.+?)}/.exec(value); + ///const match = /^{(.+?)\|(.+?)}/.exec(value); + const match = /^{(.+?)(?<=[^\\|])\|(.+?)}/.exec(value); if (!match) return; const [eaten, inlineContent, rubyText] = match; diff --git a/tests/ruby.test.ts b/tests/ruby.test.ts index 04c3019..7510fec 100644 --- a/tests/ruby.test.ts +++ b/tests/ruby.test.ts @@ -1,79 +1,36 @@ -import { stripIndent } from 'common-tags'; -import { buildProcessorTestingCode } from './utils'; +import { stringify } from '../src/index'; -it( - 'simple ruby', - buildProcessorTestingCode( - `{a|b}`, - stripIndent` - root[1] - └─0 paragraph[1] - └─0 ruby[1] - │ data: {"hName":"ruby","rubyText":"b"} - └─0 text "a" - `, - `

ab

`, - ), -); +const options = { + partial: true, + disableFormatHtml: true, +}; -it( - 'enables escape in ruby body', - buildProcessorTestingCode( - `{a\\|b|c}`, - stripIndent` - root[1] - └─0 paragraph[1] - └─0 ruby[1] - │ data: {"hName":"ruby","rubyText":"b|c"} - └─0 text "a\\\\" - `, - `

a\\b|c

`, - ), -); +it('Simple ruby', () => { + const received = stringify(`{a|b}`, options); + const expected = `

ab

`; + expect(received).toBe(expected); +}); -it( - 'disables any inline rule in ', - buildProcessorTestingCode( - `{a|*b*}`, - stripIndent` - root[1] - └─0 paragraph[1] - └─0 ruby[1] - │ data: {"hName":"ruby","rubyText":"*b*"} - └─0 text "a" - `, - `

a*b*

`, - ), -); +it('Enables escape in ruby body', () => { + const received = stringify(`{a\\|b|c}`, options); + const expected = `

a|bc

`; + expect(received).toBe(expected); +}); -it( - 'nested ruby', - buildProcessorTestingCode( - `{{a|b}|c}`, - stripIndent` - root[1] - └─0 paragraph[2] - ├─0 ruby[1] - │ │ data: {"hName":"ruby","rubyText":"b"} - │ └─0 text "{a" - └─1 text "|c}" - `, - `

{ab|c}

`, - ), -); +it('Disables any inline rule in ', () => { + const received = stringify(`{a|*b*}`, options); + const expected = `

a*b*

`; + expect(received).toBe(expected); +}); -it( - 'ruby with newline', - buildProcessorTestingCode( - `{a\nb|c}`, - stripIndent` - root[1] - └─0 paragraph[3] - ├─0 text "{a" - ├─1 break - └─2 text "b|c}" - `, - `

{a
\nb|c}

`, - { hardLineBreaks: true }, - ), -); +it('Nested ruby', () => { + const received = stringify(`{{a|b}|c}`, options); + const expected = `

{ab|c}

`; + expect(received).toBe(expected); +}); + +it('Ruby with newline', () => { + const received = stringify(`{a\nb|c}`, { ...options, hardLineBreaks: true }); + const expected = `

{a
\nb|c}

`; + expect(received).toBe(expected); +});