Skip to content

Commit

Permalink
doc: fix site render (ant-design#48514)
Browse files Browse the repository at this point in the history
* docs: fix markdown

* docs: clean up
  • Loading branch information
zombieJ authored Apr 17, 2024
1 parent 0d34e0b commit 4cefa2f
Show file tree
Hide file tree
Showing 8 changed files with 2,466 additions and 2,265 deletions.
73 changes: 63 additions & 10 deletions .dumi/theme/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { createHash } from 'crypto';
import fs from 'fs';
import path from 'path';
import { createHash } from 'crypto';
import createEmotionServer from '@emotion/server/create-instance';
import chalk from 'chalk';
import type { IApi, IRoute } from 'dumi';
import ReactTechStack from 'dumi/dist/techStacks/react';
import chalk from 'chalk';
import sylvanas from 'sylvanas';
import createEmotionServer from '@emotion/server/create-instance';

import localPackage from '../../package.json';

function extractEmotionStyle(html: string) {
Expand Down Expand Up @@ -53,13 +54,65 @@ class AntdReactTechStack extends ReactTechStack {

if (md) {
// extract description & css style from md file
const description = md.match(
new RegExp(`(?:^|\\n)## ${locale}([^]+?)(\\n## [a-z]|\\n\`\`\`|\\n<style>|$)`),
)?.[1];
const style = md.match(/\r?\n(?:```css|<style>)\r?\n([^]+?)\r?\n(?:```|<\/style>)/)?.[1];

props.description ??= description?.trim();
props.style ??= style;
const blocks: Record<string, string> = {};

const lines = md.split('\n');

let blockName = '';
let cacheList: string[] = [];

// Get block name
const getBlockName = (text: string) => {
if (text.startsWith('## ')) {
return text.replace('## ', '').trim();
}

if (text.startsWith('```css') || text.startsWith('<style>')) {
return 'style';
}

return null;
};

// Fill block content
const fillBlock = (name: string, lineList: string[]) => {
if (lineList.length) {
let fullText: string;

if (name === 'style') {
fullText = lineList
.join('\n')
.replace(/<\/?style>/g, '')
.replace(/```(\s*css)/g, '');
} else {
fullText = lineList.slice(1).join('\n');
}

blocks[name] = fullText;
}
};

for (let i = 0; i < lines.length; i++) {
const line = lines[i];

// Mark as new block
const nextBlockName = getBlockName(line);
if (nextBlockName) {
fillBlock(blockName, cacheList);

// Next Block
blockName = nextBlockName;
cacheList = [line];
} else {
cacheList.push(line);
}
}

// Last block
fillBlock(blockName, cacheList);

props.description = blocks[locale];
props.style = blocks.style;
}
}

Expand Down
Loading

0 comments on commit 4cefa2f

Please sign in to comment.