Skip to content

Commit

Permalink
todo build own remark pkg (markedjs/marked#2843 (comment))
Browse files Browse the repository at this point in the history
  • Loading branch information
nominalrune committed Aug 25, 2024
1 parent 9287ffa commit c61e5c6
Show file tree
Hide file tree
Showing 15 changed files with 16,512 additions and 10,302 deletions.
7 changes: 5 additions & 2 deletions babel.config.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
module.exports = function (api) {
api.cache(true);
return {
// presets: ['babel-preset-expo'],
plugins: ["nativewind/babel"],
presets: ['babel-preset-expo'],
plugins: [
"nativewind/babel",
"@babel/plugin-transform-unicode-escapes"
],
};
};
9 changes: 6 additions & 3 deletions components/MarkdownEdit/Blockquote.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Blockquote as BlockquoteType } from "mdast";
// import { Blockquote as BlockquoteType } from "mdast";
import { Text, View } from 'react-native';
import FlowContent from './FlowContent';
import WithText from '../../types/WithText';
export default function Blockquote({ node }: { node: WithText<BlockquoteType>;
import { Token, Tokens } from 'marked';
export default function Blockquote({ node }: {
node: Tokens.Blockquote;
prefix?: string;}) {
return <Text>
{node.children.map((item, i) => <FlowContent key={i} node={item as FlowContentType} />)}
<Text className='text-slate-400/80 font-extrabold'>{`> `}</Text>
{node.tokens.map((item, i) => <FlowContent key={i} node={item as Token} />)}
{'\n'}
</Text>;
}
5 changes: 3 additions & 2 deletions components/MarkdownEdit/Code.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Code as CodeType } from "mdast";
import { Text, useColorScheme, View } from 'react-native';
import WithText from '../../types/WithText';
export default function Code({ node}: { node: WithText<CodeType>; }) {
import { Tokens } from 'marked';
export default function Code({ node}: { node: Tokens.Code; }) {
const colorScheme = useColorScheme();
const isLight = colorScheme === 'light';
return <Text className={(isLight ? 'bg-slate-100' : 'bg-slate-700') + ' rounded p-2'}>
{node.value}
{node.raw}
</Text>;
}
6 changes: 4 additions & 2 deletions components/MarkdownEdit/FlowContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import Table from './Table';
import { Text, View } from 'react-native';
import WithText from '../../types/WithText';
import FlowContentType from '../../types/FlowContentType';
import { Token } from 'marked';

interface Prop {
node: FlowContentType;
// node: FlowContentType;
node:Token
prefix?:string
}
export default function FlowContent({ node, prefix }: Prop) {
Expand All @@ -35,6 +37,6 @@ export default function FlowContent({ node, prefix }: Prop) {
case 'table':
return <Table node={node} />;
default:
return <Text>{node.text}</Text>;
return <Text>{node.raw}</Text>;
}
}
5 changes: 3 additions & 2 deletions components/MarkdownEdit/Heading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { Text, View } from 'react-native';
import PhrasingContent from './PhrasingContent';
import WithText from '../../types/WithText';
import PhrasingContentType from '../../types/PhrasingContentType';
import { Tokens } from 'marked';

export default function Heading({ node }: { node: WithText<HeadingType>; }) {
const match = node.text.match(/^([ \t]*#+ )?(.*?)$/);
export default function Heading({ node }: { node: Tokens.Heading; }) {
const match = node.raw.match(/^([ \t]*#+ )?(.*?)$/);
const mark = match?.[1] ?? '';
const rest = match?.[2] ?? '';
return <Text className={`${node.depth === 1 ? 'text-2xl' : node.depth === 2 ? "text-xl" : "text-lg"}`}>
Expand Down
5 changes: 3 additions & 2 deletions components/MarkdownEdit/Html.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Tokens } from 'marked';
import { Html as HtmlType } from "mdast";
import { Text, View } from 'react-native';
import { WebView } from 'react-native-webview';
export default function Html({ node }: { node: HtmlType;
export default function Html({ node }: { node: Tokens.HTML|Tokens.Tag;
prefix?: string; }) {
return <Text>{node.value}\n</Text>;
return <Text>{node.raw}\n</Text>;
}
25 changes: 16 additions & 9 deletions components/MarkdownEdit/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,53 @@ import { FlatList, Text, View } from 'react-native';
import Paragraph from './Paragraph';
import { useState } from 'react';
import WithText from '../../types/WithText';
export default function List({ node, prefix }: { node: WithText<ListType>; prefix?: string; }) {
import { Tokens } from 'marked';
export default function List({ node, prefix }: { node: Tokens.List; prefix?: string; }) {
// return <Text>{node.text}</Text>
return <Text className="flex flex-col justify-start">
{
node.children.map((item, i) => <ListItem key={i} node={item} prefix={prefix} />)
node.items.map((item, i) => <ListItem key={i} node={item} prefix={prefix} />)
}
</Text>;
}

function ListItem({ node, prefix: _prefix = '' }: { node: WithText<ListItemType>; prefix?: string; }) {
function ListItem({ node, prefix: _prefix = '' }: { node: Tokens.ListItem; prefix?: string; }) {
const matchReg = /^([ \t]*)((?:[\-\*]|(?:\d+.)) )?(\[[x ]\] )?(.*)(?:¥n(.*?))?$/s;
const match = node.text.match(matchReg);
const match = node.raw.match(matchReg);
const prefix = match?.[1] ?? '';
const mark = match?.[2] ?? '';
const checkmark = match?.[3] ?? '';
const rest = match?.[4] ?? "";
const nextPrefix = (match?.[5] ?? "").match(matchReg)?.[1]??'';
const children = [...node.children];
const children = [...node.tokens];
const paragraph = children.shift();
const itemText = paragraph?.type==='paragraph' ? <Paragraph node={paragraph} /> : <Text>{`\n`}</Text>;
// return <Text>{node.text}{`\n`}</Text>
// console.log('LISTITEM: text:' + node.text, '_prefix|' + _prefix + '|', 'prefix|' + prefix + '|');
return <>
<Text className='text-slate-400/80 font-extrabold'>
{_prefix}
{prefix}
{/* {prefix} */}
{mark}
{node.checked !== null && (checkmark === '[x] '
{node.task && (node.checked
? <>[<Text className='text-green-600'>x</Text>] </>
: <>{checkmark}</>
)}
</Text>
{itemText}
{node.text}
{/* {itemText} */}
{/* {(match?.[5] ?? "")} */}
{
node.tokens.map((item, i) => <Text key={i}>
<FlowContent node={item} prefix={_prefix + prefix+ ' '} />
</Text>)
}
{/* {
!!children.length
? children.map((item, i) => <Text key={i}>
<FlowContent node={item} prefix={_prefix + prefix+ ' '} />
</Text>)
: <></>
}
} */}
</>;
}
5 changes: 3 additions & 2 deletions components/MarkdownEdit/Paragraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { Text, View } from 'react-native';
import PhrasingContent from './PhrasingContent';
import { Fragment } from 'react';
import WithText from '../../types/WithText';
import { Tokens } from 'marked';
export default function Paragraph({ node, prefix='' }: {
node: WithText<ParagraphType>;
node: Tokens.Paragraph;
prefix?:string;
}) {
return <Text>{
node.children.map((child,i) => <Fragment key={i}>
node.tokens.map((child,i) => <Fragment key={i}>
<Text>{prefix}</Text>
<PhrasingContent node={child} />
</Fragment>)
Expand Down
17 changes: 9 additions & 8 deletions components/MarkdownEdit/PhrasingContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ import Link from './Link';
import InlineCode from './InlineCode';
import WithText from '../../types/WithText';
import PhrasingContentType from '../../types/PhrasingContentType';
import { MarkedToken, Token } from 'marked';

interface Prop {
node: PhrasingContentType;
node: MarkedToken;
}
export default function PhrasingContent({ node }: Prop) {
// return <Text>{node.text}</Text>;
switch (node.type) {
case 'break':
return <Text area-type={node.type}>{node.text}</Text>;
case 'delete': // TODO
return <Text>~~{node.children.map(item => <PhrasingContent node={item as PhrasingContentType} />)}~~</Text>;
case 'emphasis':
// case 'break':
// return <Text area-type={node.type}>{node.text}</Text>;
// case 'delete': // TODO
// return <Text>~~{node.tokens.map(item => <PhrasingContent node={item as PhrasingContentType} />)}~~</Text>;
case 'em':
case 'strong':
return <Text><Strong node={node} /></Text>;
case 'html':
Expand All @@ -29,8 +30,8 @@ export default function PhrasingContent({ node }: Prop) {
return <Image node={node} />;
case 'link':
return <Link node={node} />;
case 'inlineCode':
return <InlineCode node={node} />;
// case 'inlineCode':
// return <InlineCode node={node} />;
case 'text':
return <Text>{node.text}</Text>;
default:
Expand Down
4 changes: 2 additions & 2 deletions components/MarkdownEdit/Root.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Root as RootType } from "mdast";
// import { Root as RootType } from "mdast";
import { NativeSyntheticEvent, ScrollView, Text, TextInput, TextInputChangeEventData, TextInputKeyPressEventData, View } from 'react-native';
import { RefObject, useEffect, useMemo, useRef, useState } from 'react';
import Parser from '../../services/Markdown/Parser';
Expand Down Expand Up @@ -54,7 +54,7 @@ export default function Root({ content, update, setSelection }:Prop ) {
onSelectionChange={handleSelectionChange}
onKeyPress={handlePressed}
>
{contentNode.children?.map((node, i) => <FlowContent key={i} node={node} />)}
{contentNode?.map((node, i) => <FlowContent key={i} node={node} />)}
</TextInput>
</View>;
}
3 changes: 2 additions & 1 deletion components/MarkdownEdit/Strong.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { Text, View } from 'react-native';
import PhrasingContent from './PhrasingContent';
import WithText from '../../types/WithText';
import PhrasingContentType from '../../types/PhrasingContentType';
export default function Strong({ node }: { node: WithText<StrongType> | WithText<Emphasis>; }) {
import { Tokens } from 'marked';
export default function Strong({ node }: { node: Tokens.Em | Tokens.Strong; }) {
// return <Text className='font-semibold'>{node.text}</Text>
const match = node.text.match(/((?:\*\*)|(?:__)).*?((?:\*\*)|(?:__))/);
const prefix = match?.[1] ?? '';
Expand Down
Loading

1 comment on commit c61e5c6

@expo
Copy link

@expo expo bot commented on c61e5c6 Aug 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GitHub Build Trigger Failure

The build trigger failed with the following error:

INVALID_EAS_JSON_ERROR: eas.json not found in the repository.

Please check your GitHub app installation settings and your Expo project's GitHub settings to make sure you've configured everything correctly.

Please sign in to comment.