Skip to content

Commit

Permalink
chore: $ npm run format
Browse files Browse the repository at this point in the history
  • Loading branch information
progfay committed Jun 13, 2021
1 parent e22a59a commit 0d455d2
Show file tree
Hide file tree
Showing 56 changed files with 1,087 additions and 1,004 deletions.
6 changes: 3 additions & 3 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: ''
assignees: ''
title: ""
labels: ""
assignees: ""
---

**Describe the bug**
Expand Down
6 changes: 3 additions & 3 deletions .github/ISSUE_TEMPLATE/custom.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: Custom issue template
about: Describe this issue template's purpose here.
title: ''
labels: ''
assignees: ''
title: ""
labels: ""
assignees: ""
---
6 changes: 3 additions & 3 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
name: Feature request
about: Suggest an idea for this project
title: ''
labels: ''
assignees: ''
title: ""
labels: ""
assignees: ""
---

**Is your feature request related to a problem? Please describe.**
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ $ npm i @progfay/scrapbox-parser
## Usage

```js
import { parse } from '@progfay/scrapbox-parser'
import fetch from 'node-fetch'
import { parse } from "@progfay/scrapbox-parser";
import fetch from "node-fetch";

const PROJECT_NAME = 'help'
const PAGE_NAME = 'syntax'
const PROJECT_NAME = "help";
const PAGE_NAME = "syntax";

fetch(`https://scrapbox.io/api/pages/${PROJECT_NAME}/${PAGE_NAME}/text`)
.then(response => response.text())
.then(text => parse(text))
.then((response) => response.text())
.then((text) => parse(text));
```
32 changes: 17 additions & 15 deletions src/block/CodeBlock.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import type { Row } from './Row'
import type { Row } from "./Row";

export interface CodeBlockPack {
type: 'codeBlock'
rows: Row[]
type: "codeBlock";
rows: Row[];
}

export interface CodeBlock {
indent: number
type: 'codeBlock'
fileName: string
content: string
indent: number;
type: "codeBlock";
fileName: string;
content: string;
}

export const convertToCodeBlock = (pack: CodeBlockPack): CodeBlock => {
const {
rows: [head, ...body]
} = pack
const { indent = 0, text = '' } = head ?? {}
const fileName: string = text.replace(/^\s*code:/, '')
rows: [head, ...body],
} = pack;
const { indent = 0, text = "" } = head ?? {};
const fileName: string = text.replace(/^\s*code:/, "");

return {
indent,
type: 'codeBlock',
type: "codeBlock",
fileName,
content: body.map((row: Row): string => row.text.substring(indent + 1)).join('\n')
}
}
content: body
.map((row: Row): string => row.text.substring(indent + 1))
.join("\n"),
};
};
26 changes: 13 additions & 13 deletions src/block/Line.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { convertToNodes } from './node'
import { convertToNodes } from "./node";

import type { Row } from './Row'
import type { Node } from './node/type'
import type { Row } from "./Row";
import type { Node } from "./node/type";

export interface LinePack {
type: 'line'
rows: [Row]
type: "line";
rows: [Row];
}

export interface Line {
indent: number
type: 'line'
nodes: Node[]
indent: number;
type: "line";
nodes: Node[];
}

export const convertToLine = (pack: LinePack): Line => {
const { indent, text } = pack.rows[0]
const { indent, text } = pack.rows[0];
return {
indent,
type: 'line',
nodes: convertToNodes(text.substring(indent))
}
}
type: "line",
nodes: convertToNodes(text.substring(indent)),
};
};
53 changes: 29 additions & 24 deletions src/block/Pack.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,47 @@
import type { ParserOption } from '../parse'
import type { CodeBlockPack } from './CodeBlock'
import type { LinePack } from './Line'
import type { Row } from './Row'
import type { TablePack } from './Table'
import type { TitlePack } from './Title'
import type { ParserOption } from "../parse";
import type { CodeBlockPack } from "./CodeBlock";
import type { LinePack } from "./Line";
import type { Row } from "./Row";
import type { TablePack } from "./Table";
import type { TitlePack } from "./Title";

export type Pack = TitlePack | CodeBlockPack | TablePack | LinePack
export type Pack = TitlePack | CodeBlockPack | TablePack | LinePack;

const isChildRowOfPack = (pack: Pack, row: Row): boolean =>
(pack.type === 'codeBlock' || pack.type === 'table') && row.indent > (pack.rows[0]?.indent ?? 0)
(pack.type === "codeBlock" || pack.type === "table") &&
row.indent > (pack.rows[0]?.indent ?? 0);

const packing = (packs: Pack[], row: Row): Pack[] => {
const lastPack = packs[packs.length - 1]
const lastPack = packs[packs.length - 1];
if (lastPack !== undefined && isChildRowOfPack(lastPack, row)) {
lastPack.rows.push(row)
return packs
lastPack.rows.push(row);
return packs;
}

packs.push({
type: /^\s*code:/.test(row.text) ? 'codeBlock' : /^\s*table:/.test(row.text) ? 'table' : 'line',
rows: [row]
})
type: /^\s*code:/.test(row.text)
? "codeBlock"
: /^\s*table:/.test(row.text)
? "table"
: "line",
rows: [row],
});

return packs
}
return packs;
};

export const packRows = (rows: Row[], opts: ParserOption): Pack[] => {
if (opts.hasTitle ?? true) {
const [title, ...body] = rows
if (title === undefined) return []
const [title, ...body] = rows;
if (title === undefined) return [];
return [
{
type: 'title',
rows: [title]
type: "title",
rows: [title],
},
...body.reduce(packing, [])
]
...body.reduce(packing, []),
];
}

return rows.reduce(packing, [])
}
return rows.reduce(packing, []);
};
10 changes: 5 additions & 5 deletions src/block/Row.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export interface Row {
indent: number
text: string
indent: number;
text: string;
}

export const parseToRows = (input: string): Row[] =>
input.split('\n').map(text => ({
input.split("\n").map((text) => ({
indent: /^\s+/.exec(text)?.[0]?.length ?? 0,
text
}))
text,
}));
40 changes: 21 additions & 19 deletions src/block/Table.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
import { convertToNodes } from './node'
import { convertToNodes } from "./node";

import type { Row } from './Row'
import type { Node } from './node/type'
import type { Row } from "./Row";
import type { Node } from "./node/type";

export interface TablePack {
type: 'table'
rows: Row[]
type: "table";
rows: Row[];
}

export interface Table {
indent: number
type: 'table'
fileName: string
cells: Node[][][]
indent: number;
type: "table";
fileName: string;
cells: Node[][][];
}

export const convertToTable = (pack: TablePack): Table => {
const {
rows: [head, ...body]
} = pack
const { indent = 0, text = '' } = head ?? {}
const fileName = text.replace(/^\s*table:/, '')
rows: [head, ...body],
} = pack;
const { indent = 0, text = "" } = head ?? {};
const fileName = text.replace(/^\s*table:/, "");

return {
indent,
type: 'table',
type: "table",
fileName,
cells: body
.map((row: Row): string => row.text.substring(indent + 1))
.map((text: string): Node[][] =>
text
.split('\t')
.map((block: string): Node[] => convertToNodes(block, { nested: true, quoted: false }))
)
}
}
.split("\t")
.map((block: string): Node[] =>
convertToNodes(block, { nested: true, quoted: false })
)
),
};
};
18 changes: 9 additions & 9 deletions src/block/Title.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import type { Row } from './Row'
import type { Row } from "./Row";

export interface TitlePack {
type: 'title'
rows: [Row]
type: "title";
rows: [Row];
}

export interface Title {
type: 'title'
text: string
type: "title";
text: string;
}

export const convertToTitle = (pack: TitlePack): Title => {
return {
type: 'title',
text: pack.rows[0].text
}
}
type: "title",
text: pack.rows[0].text,
};
};
38 changes: 19 additions & 19 deletions src/block/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { convertToTitle } from './Title'
import { convertToCodeBlock } from './CodeBlock'
import { convertToTable } from './Table'
import { convertToLine } from './Line'
import { convertToTitle } from "./Title";
import { convertToCodeBlock } from "./CodeBlock";
import { convertToTable } from "./Table";
import { convertToLine } from "./Line";

import type { Pack } from './Pack'
import type { Title } from './Title'
import type { CodeBlock } from './CodeBlock'
import type { Table } from './Table'
import type { Line } from './Line'
import type { Pack } from "./Pack";
import type { Title } from "./Title";
import type { CodeBlock } from "./CodeBlock";
import type { Table } from "./Table";
import type { Line } from "./Line";

export type Block = Title | CodeBlock | Table | Line
export type Block = Title | CodeBlock | Table | Line;

export const convertToBlock = (pack: Pack): Block => {
switch (pack.type) {
case 'title':
return convertToTitle(pack)
case "title":
return convertToTitle(pack);

case 'codeBlock':
return convertToCodeBlock(pack)
case "codeBlock":
return convertToCodeBlock(pack);

case 'table':
return convertToTable(pack)
case "table":
return convertToTable(pack);

case 'line':
return convertToLine(pack)
case "line":
return convertToLine(pack);
}
}
};
18 changes: 9 additions & 9 deletions src/block/node/BlankNode.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { createNodeParser } from './creator'
import { createNodeParser } from "./creator";

import type { BlankNode } from './type'
import type { NodeCreator } from './creator'
import type { BlankNode } from "./type";
import type { NodeCreator } from "./creator";

const blankRegExp = /\[\s+\]/
const blankRegExp = /\[\s+\]/;

const createBlankNode: NodeCreator<BlankNode> = (raw: string) => ({
type: 'blank',
type: "blank",
raw,
text: raw.substring(1, raw.length - 1)
})
text: raw.substring(1, raw.length - 1),
});

export const BlankNodeParser = createNodeParser(createBlankNode, {
parseOnNested: false,
parseOnQuoted: true,
patterns: [blankRegExp]
})
patterns: [blankRegExp],
});
Loading

0 comments on commit 0d455d2

Please sign in to comment.