Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsers #614

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions apps/spa/components/entities/EntityStrong.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import type { FC } from 'react';
import type { Strong } from '../../interpreter/entities';
import { EntityEmphasis } from './EntityEmphasis';

interface Props {
source: string;
entity: Strong;
}

export const EntityStrong: FC<Props> = ({
source,
entity: {
child: { start, len },
},
}) => {
return <strong className="EntityStrong">{source.substring(start, start + len)}</strong>;
export const EntityStrong: FC<Props> = ({ source, entity: { child } }) => {
const content =
child.type === 'Text' ? (
source.substring(child.start, child.start + child.len)
) : (
<EntityEmphasis source={source} entity={child} />
);
return <strong className="EntityStrong">{content}</strong>;
};
2 changes: 1 addition & 1 deletion apps/spa/interpreter/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface CodeBlock extends BaseEntity {

export interface Strong extends BaseEntity {
type: 'Strong';
child: Text;
child: Text | Emphasis;
}

export interface Emphasis extends BaseEntity {
Expand Down
38 changes: 36 additions & 2 deletions apps/spa/interpreter/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,29 @@ const strong: P<Entity> = regex(STRONG_REGEX).then(([match, { text, rest }]) =>
return [entity, { text, rest }];
});

const STRONG_EM_REGEX = /^\*\*\*(.+?)\*\*\*/;

const strongEmphasis: P<Entity> = regex(STRONG_EM_REGEX).then(([match, { text, rest }]) => {
const [entire, content = ''] = match;
const entity: Strong = {
type: 'Strong',
start: text.length,
len: entire.length,
child: {
type: 'Emphasis',
start: text.length,
len: entire.length,
child: {
type: 'Text',
start: text.length + entire.indexOf(content),
len: content.length,
},
},
};
text += entire;
return [entity, { text, rest }];
});

const URL_REGEX = /^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&/=]*)/;

const autoUrl: P<Entity> = regex(URL_REGEX).then(([match, { text, rest }]) => {
Expand Down Expand Up @@ -610,7 +633,7 @@ const exprNodeToEntity =

const ROLL_COMMAND = /^[.。]r\s*/;

const entity = choice<Entity>([codeBlock, code, strong, emphasis, link, autoUrl, expression, span]);
const entity = choice<Entity>([codeBlock, code, strongEmphasis, strong, emphasis, link, autoUrl, expression, span]);

const message: P<Entity[]> = many(entity).map((entityList) => entityList.reduce(mergeTextEntitiesReducer, []));

Expand All @@ -622,7 +645,18 @@ const rollCommand: P<Entity[]> = new P((state, env) => {
}
return exprNodeToEntity(state)(result);
});
const entity = choice<Entity>([codeBlock, code, strong, emphasis, link, autoUrl, expression, exprEntity, span]);
const entity = choice<Entity>([
codeBlock,
code,
strongEmphasis,
strong,
emphasis,
link,
autoUrl,
expression,
exprEntity,
span,
]);
const message = many(entity).map((entityList) => entityList.reduce(mergeTextEntitiesReducer, []));
return message.run(state, env);
});
Expand Down