mirror of
https://github.com/usememos/memos.git
synced 2024-12-19 09:02:49 +03:00
feat: add inline-code
syntax parser
This commit is contained in:
parent
2298ac6ff3
commit
7b29c65f58
@ -82,6 +82,18 @@ console.log("hello world!")
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
for (const t of tests) {
|
||||||
|
expect(marked(t.markdown)).toBe(t.want);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
test("parse inline code", () => {
|
||||||
|
const tests = [
|
||||||
|
{
|
||||||
|
markdown: `Code: \`console.log("Hello world!")\``,
|
||||||
|
want: `<p>Code: <code>console.log("Hello world!")</code></p>`,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
for (const t of tests) {
|
for (const t of tests) {
|
||||||
expect(marked(t.markdown)).toBe(t.want);
|
expect(marked(t.markdown)).toBe(t.want);
|
||||||
}
|
}
|
||||||
|
23
web/src/labs/marked/parser/InlineCode.ts
Normal file
23
web/src/labs/marked/parser/InlineCode.ts
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
export const INLINE_CODE_REG = /`([\S ]+?)`/;
|
||||||
|
|
||||||
|
const match = (rawStr: string): number => {
|
||||||
|
const matchResult = rawStr.match(INLINE_CODE_REG);
|
||||||
|
if (!matchResult) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
const matchStr = matchResult[0];
|
||||||
|
return matchStr.length;
|
||||||
|
};
|
||||||
|
|
||||||
|
const renderer = (rawStr: string): string => {
|
||||||
|
const parsedStr = rawStr.replace(INLINE_CODE_REG, "<code>$1</code>");
|
||||||
|
return parsedStr;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default {
|
||||||
|
name: "inline code",
|
||||||
|
regex: INLINE_CODE_REG,
|
||||||
|
match,
|
||||||
|
renderer,
|
||||||
|
};
|
@ -11,6 +11,7 @@ import Mark from "./Mark";
|
|||||||
import Bold from "./Bold";
|
import Bold from "./Bold";
|
||||||
import Emphasis from "./Emphasis";
|
import Emphasis from "./Emphasis";
|
||||||
import PlainLink from "./PlainLink";
|
import PlainLink from "./PlainLink";
|
||||||
|
import InlineCode from "./InlineCode";
|
||||||
|
|
||||||
export { CODE_BLOCK_REG } from "./CodeBlock";
|
export { CODE_BLOCK_REG } from "./CodeBlock";
|
||||||
export { TODO_LIST_REG } from "./TodoList";
|
export { TODO_LIST_REG } from "./TodoList";
|
||||||
@ -27,5 +28,5 @@ export { EMPHASIS_REG } from "./Emphasis";
|
|||||||
|
|
||||||
// The order determines the order of execution.
|
// The order determines the order of execution.
|
||||||
export const blockElementParserList = [CodeBlock, TodoList, DoneList, OrderedList, UnorderedList, Paragraph];
|
export const blockElementParserList = [CodeBlock, TodoList, DoneList, OrderedList, UnorderedList, Paragraph];
|
||||||
export const inlineElementParserList = [Image, Mark, Link, Bold, Emphasis, PlainLink, Tag];
|
export const inlineElementParserList = [Image, Mark, Link, Bold, Emphasis, InlineCode, PlainLink, Tag];
|
||||||
export const parserList = [...blockElementParserList, ...inlineElementParserList];
|
export const parserList = [...blockElementParserList, ...inlineElementParserList];
|
||||||
|
@ -48,7 +48,11 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
pre {
|
pre {
|
||||||
@apply w-full mt-1 py-2 px-3 rounded text-sm bg-gray-100 whitespace-pre-wrap;
|
@apply w-full my-1 py-2 px-3 rounded text-sm bg-gray-100 whitespace-pre-wrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
code {
|
||||||
|
@apply bg-gray-100 px-1 rounded text-sm leading-6 inline-block;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user