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

Feat: Markdown adapter #30

Merged
merged 4 commits into from
Oct 22, 2024
Merged
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
2 changes: 1 addition & 1 deletion packages/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@junyiacademy/perseus-core",
"version": "1.0.44",
"version": "1.0.46",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"license": "MIT",
Expand Down
8 changes: 4 additions & 4 deletions packages/perseus/src/perseus-markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,10 @@ export default {
traverseContent: traverseContent,
parse: parse,
parseInline: inlineParser,
reactFor: SimpleMarkdown.reactFor,
ruleOutput: SimpleMarkdown.ruleOutput(rules, "react") as any,
sanitizeUrl: SimpleMarkdown.sanitizeUrl,
reactFor: SimpleMarkdown.reactFor, // deprecated
ruleOutput: SimpleMarkdown.ruleOutput(rules, "react") as any, // deprecated
basicOutput: SimpleMarkdown.reactFor(
SimpleMarkdown.ruleOutput(rules, "react"),
) as any,
sanitizeUrl: SimpleMarkdown.sanitizeUrl,
) as any, // deprecated
};
12 changes: 8 additions & 4 deletions packages/simple-markdown/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* Many of the regexes and original logic has been adapted from
* the wonderful [marked.js](https://github.com/chjj/marked)
*/
import {adaptLegacyMarkdown} from "./legacyMarkdownAdapter";
import type {Capture, MatchFunction, State} from "./troublesome-types";
import type * as React from "react";

Expand Down Expand Up @@ -450,17 +451,20 @@ var parserFor = function (
source: string,
state?: State | null,
): Array<SingleASTNode> {
var legacyAdaptedStr = adaptLegacyMarkdown(source);
latestState = populateInitialState(state, defaultState);
if (!latestState.inline && !latestState.disableAutoBlockNewlines) {
source = source + "\n\n";
}
var str =
!latestState.inline && !latestState.disableAutoBlockNewlines
? legacyAdaptedStr + "\n\n"
: legacyAdaptedStr;

// We store the previous capture so that match functions can
// use some limited amount of lookbehind. Lists use this to
// ensure they don't match arbitrary '- ' or '* ' in inline
// text (see the list rule for more information). This stores
// the full regex capture object, if there is one.
latestState.prevCapture = null;
return nestedParse(preprocess(source), latestState);
return nestedParse(preprocess(str), latestState);
};

return outerParse;
Expand Down
19 changes: 19 additions & 0 deletions packages/simple-markdown/src/legacyMarkdownAdapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const adaptLegacyMarkdown = (str: string) => {
// heading 前只有一個 \n 時,將 \n 改為 \n\n
const addedNewLineAboveHeadingStr = str.replace(
/(?<=[^\n]\n *)(#{1,6}.*)/g,
"\n$1",
);
// heading 後只有一個 \n 時,將 \n 改為 \n\n
const addedNewLineUnderHeadingStr = addedNewLineAboveHeadingStr.replace(
/(?<=\n *|^)(#{1,6}[^\n]*)(?=\n[^\n])/g,
"$1\n",
);
// hr 後只有一個 \n 時,將 \n 改為 \n\n
const addedNewLineUnderHrStr = addedNewLineUnderHeadingStr.replace(
/(?<=\n *)([-*_]{3,})(?=\n[^\n])/g,
"$1\n",
);

return addedNewLineUnderHrStr;
};