Skip to content

Commit

Permalink
numbered list: custom component
Browse files Browse the repository at this point in the history
  • Loading branch information
hayzamjs committed Nov 28, 2024
1 parent d836114 commit 2ca2cdf
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 6 deletions.
78 changes: 78 additions & 0 deletions embedded/rspress/src/components/NumberedListItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import React from 'react';
import { getColorClass } from '../utils/style';

interface TextSegment {
type: 'text' | 'link';
text?: string;
href?: string;
content?: TextSegment[];
styles?: {
bold?: boolean;
textColor?: string;
strike?: boolean;
backgroundColor?: string;
};
}

interface NumberedListItemData {
id: string;
type: 'numberedListItem';
props: {
textColor?: string;
backgroundColor?: string;
textAlignment?: 'left' | 'center' | 'right';
};
content: TextSegment[];
children: any[];
}

interface NumberedListItemProps {
rawJson: NumberedListItemData;
}

export const NumberedListItem: React.FC<NumberedListItemProps> = ({ rawJson }) => {
const { props, content, children } = rawJson;

const containerClasses = [
getColorClass(props.textColor),
getColorClass(props.backgroundColor, true),
props.textAlignment === 'left' ? 'kal-text-left' :
props.textAlignment === 'center' ? 'kal-text-center' :
props.textAlignment === 'right' ? 'kal-text-right' : '',
'kal-mb-2',
].filter(Boolean).join(' ');

const renderTextSegment = (segment: TextSegment, index: number): React.ReactNode => {
const segmentClasses = [
segment.styles?.bold ? 'kal-font-bold' : '',
getColorClass(segment.styles?.textColor),
getColorClass(segment.styles?.backgroundColor, true),
segment.styles?.strike ? 'kal-line-through' : ''
].filter(Boolean).join(' ');

if (segment.type === 'link') {
return (
<a key={index} href={segment.href} className={`${segmentClasses} kal-underline kal-text-blue-500`}>
{segment.content?.map((subSegment, subIndex) =>
renderTextSegment(subSegment, subIndex)
) || segment.text}
</a>
);
}

return (
<span key={index} className={segmentClasses}>
{segment.text}
</span>
);
};

return (
<li className={containerClasses}>
{content.map((segment, index) => renderTextSegment(segment, index))}
{children}
</li>
);
};

export default NumberedListItem;
5 changes: 4 additions & 1 deletion services/docs_rspress.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (service *DocService) GenerateHead(docID uint, pageId uint, pageType string

buffer.WriteString("import { Meta } from '@components/Meta';\n")

componentTypes := []string{"paragraph", "table", "image", "video", "audio", "file", "alert"}
componentTypes := []string{"paragraph", "table", "image", "video", "audio", "file", "alert", "numberedListItem", "bulletListItem"}
caser := cases.Title(language.English)
addedComponents := make(map[string]bool)
var contentObjects []map[string]interface{}
Expand All @@ -152,6 +152,9 @@ func (service *DocService) GenerateHead(docID uint, pageId uint, pageType string
for _, componentType := range componentTypes {
if objType == componentType && !addedComponents[componentType] {
componentName := caser.String(componentType)
if componentName == "Numberedlistitem" {
componentName = "NumberedListItem"
}
buffer.WriteString(fmt.Sprintf(`import { %s } from "@components/%s";%s`, componentName, componentName, "\n"))
addedComponents[componentType] = true
}
Expand Down
6 changes: 1 addition & 5 deletions utils/json_markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,11 @@ func BlockToMarkdown(block Block, depth int, numbering *[]int) string {
switch block.Type {
case "heading":
return HeadingToMarkdown(block)
case "numberedListItem":
return numberedListItemToMarkdown(block, depth, numbering, styledContent)
case "bulletListItem":
return bulletListItemToMarkdown(block, depth, styledContent)
case "checkListItem":
return checkListItemToMarkdown(block, depth, styledContent)
case "procode":
return ProcodeToMarkdown(block.Props)
case "paragraph", "table", "image", "video", "audio", "file", "alert":
case "paragraph", "table", "image", "video", "audio", "file", "alert", "numberedListItem", "bulletListItem":
return BlockToMDX(block)
default:
return ""
Expand Down

0 comments on commit 2ca2cdf

Please sign in to comment.