Skip to content

Commit

Permalink
fix: Fixed an issue where ruby couldn't escape pipe with a backslash
Browse files Browse the repository at this point in the history
  • Loading branch information
akabekobeko committed Jul 6, 2021
1 parent cf11305 commit ac111a2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 77 deletions.
19 changes: 17 additions & 2 deletions docs/vfm.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -169,8 +170,6 @@ figure figcaption {

## Ruby

<Badge type="warning">PRE-RELEASE</Badge>

**VFM**

```
Expand All @@ -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
<p><ruby>a|b<rt>c</rt></ruby></p>
```

## Sectionization

Make the heading a hierarchical section.
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/ruby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
105 changes: 31 additions & 74 deletions tests/ruby.test.ts
Original file line number Diff line number Diff line change
@@ -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"
`,
`<p><ruby>a<rt>b</rt></ruby></p>`,
),
);
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\\\\"
`,
`<p><ruby>a\\<rt>b|c</rt></ruby></p>`,
),
);
it('Simple ruby', () => {
const received = stringify(`{a|b}`, options);
const expected = `<p><ruby>a<rt>b</rt></ruby></p>`;
expect(received).toBe(expected);
});

it(
'disables any inline rule in <rt>',
buildProcessorTestingCode(
`{a|*b*}`,
stripIndent`
root[1]
└─0 paragraph[1]
└─0 ruby[1]
│ data: {"hName":"ruby","rubyText":"*b*"}
└─0 text "a"
`,
`<p><ruby>a<rt>*b*</rt></ruby></p>`,
),
);
it('Enables escape in ruby body', () => {
const received = stringify(`{a\\|b|c}`, options);
const expected = `<p><ruby>a|b<rt>c</rt></ruby></p>`;
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}"
`,
`<p><ruby>{a<rt>b</rt></ruby>|c}</p>`,
),
);
it('Disables any inline rule in <rt>', () => {
const received = stringify(`{a|*b*}`, options);
const expected = `<p><ruby>a<rt>*b*</rt></ruby></p>`;
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}"
`,
`<p>{a<br>\nb|c}</p>`,
{ hardLineBreaks: true },
),
);
it('Nested ruby', () => {
const received = stringify(`{{a|b}|c}`, options);
const expected = `<p><ruby>{a<rt>b</rt></ruby>|c}</p>`;
expect(received).toBe(expected);
});

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

0 comments on commit ac111a2

Please sign in to comment.