Skip to content

Commit

Permalink
Merge branch 'develop' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
marihachi committed Apr 17, 2021
2 parents d238c1d + e412bcd commit 027edb4
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 9 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,15 @@ npm run build
```

### Use the interactive CLI parser
full parser:
```
npm run parse
```

plain parser:
```
npm run parse-plain
```

## License
This software is released under the [MIT License](LICENSE).
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mfm-js",
"version": "0.15.0",
"version": "0.15.1",
"description": "An MFM parser implementation with PEG.js",
"main": "./built/index.js",
"types": "./built/index.d.ts",
Expand Down
16 changes: 10 additions & 6 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import peg from 'pegjs';
import { MfmNode, MfmPlainNode } from './node';
import { stringifyNode, stringifyTree } from './util';
import { stringifyNode, stringifyTree, inspectOne } from './util';

const parser: peg.Parser = require('./parser');

Expand Down Expand Up @@ -37,13 +37,17 @@ export function toString(node: MfmNode | MfmNode[]): string {
/**
* Inspects the MfmNode tree.
*/
export function inspect(nodes: MfmNode[], action: (node: MfmNode) => void): void {
for (const node of nodes) {
action(node);
if (node.children != null) {
inspect(node.children, action);
export function inspect(node: MfmNode, action: (node: MfmNode) => void): void
export function inspect(nodes: MfmNode[], action: (node: MfmNode) => void): void
export function inspect(node: (MfmNode | MfmNode[]), action: (node: MfmNode) => void): void {
if (Array.isArray(node)) {
for (const n of node) {
inspectOne(n, action);
}
}
else {
inspectOne(node, action);
}
}

/**
Expand Down
15 changes: 13 additions & 2 deletions src/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,26 @@ block
// block: quote

quote
= lines:quoteLine+
= head:quoteMultiLine tails:quoteMultiLine+
{
const children = applyParser(lines.join('\n'), 'fullParser');
const children = applyParser([head, ...tails].join('\n'), 'fullParser');
return QUOTE(children);
}
/ line:quoteLine
{
const children = applyParser(line, 'fullParser');
return QUOTE(children);
}

quoteMultiLine
= quoteLine / quoteEmptyLine

quoteLine
= BEGIN ">" _? text:$(CHAR+) END { return text; }

quoteEmptyLine
= BEGIN ">" _? END { return ''; }

// block: search

search
Expand Down
9 changes: 9 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ export function stringifyTree(nodes: MfmNode[]): string {
return dest.map(n => stringifyNode(n)).join('');
}

export function inspectOne(node: MfmNode, action: (node: MfmNode) => void) {
action(node);
if (node.children != null) {
for (const child of node.children) {
inspectOne(child, action);
}
}
}

//
// dynamic consuming
//
Expand Down
11 changes: 11 additions & 0 deletions test/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ after`;
});
assert.strictEqual(mfm.toString(result), 'hello [tada everynyan!]');
});

it('replace text (one item)', () => {
const input = 'good morning [tada everyone!]';
const result = mfm.parse(input);
mfm.inspect(result[1], node => {
if (node.type == 'text') {
node.props.text = node.props.text.replace(/one/g, 'nyan');
}
});
assert.strictEqual(mfm.toString(result), 'good morning [tada everynyan!]');
});
});

describe('extract', () => {
Expand Down
20 changes: 20 additions & 0 deletions test/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,26 @@ describe('FullParser', () => {
];
assert.deepStrictEqual(mfm.parse(input), output);
});
it('複数行の引用ブロックでは空行を含めることができる', () => {
const input = `
> abc
>
> 123
`;
const output = [
QUOTE([
TEXT('abc\n\n123')
])
];
assert.deepStrictEqual(mfm.parse(input), output);
});
it('1行の引用ブロックを空行にはできない', () => {
const input = `> `;
const output = [
TEXT('> ')
];
assert.deepStrictEqual(mfm.parse(input), output);
});
});

describe('search', () => {
Expand Down

0 comments on commit 027edb4

Please sign in to comment.