import { defaultSystem } from "@chakra-ui/react/preset" import { markdownTable } from "markdown-table" const { _config: config, tokens } = defaultSystem export function getKeyframesDoc() { const allKeyframes = Object.keys(config.theme?.keyframes || {}) return markdownTable([ ["Animation Key", "Example"], ...allKeyframes.map((name) => [ `\`${name}\``, ``, ]), ]) } export function getDurationsDoc() { const allDurations = Array.from( tokens.categoryMap.get("durations")!.values(), ).sort((a, b) => parseFloat(b.originalValue) - parseFloat(a.originalValue)) return markdownTable([ ["Duration Token", "Value", "Example"], ...allDurations.map((token) => [ `\`${token.extensions.prop}\``, `\`${token.originalValue}\``, ``, ]), ]) } export function getAspectRatiosDoc() { const aspectRatios = tokens.categoryMap.get("aspectRatios")! const allAspectRatios = Array.from(aspectRatios.values()) return markdownTable([ ["Aspect Ratio Token", "Value", "Example"], ...allAspectRatios.map((token) => [ `\`${token.extensions.prop}\``, `\`${token.value}\``, ``, ]), ]) } export function getCursorDoc() { const cursor = tokens.categoryMap.get("cursor")! const allCursorTokens = Array.from(cursor.values()) return markdownTable([ ["Cursor Token", "Value", "Example"], ...allCursorTokens.map((token) => [ `\`${token.extensions.prop}\``, `\`${token.value}\``, ``, ]), ]) } export function getBreakpointsDoc() { const breakpoints = config.theme?.breakpoints || {} const allBreakpoints = Object.entries(breakpoints) .sort((a, b) => parseFloat(a[1]) - parseFloat(b[1])) .map(([key]) => key) return markdownTable([ ["Breakpoint Token", "Example"], ...allBreakpoints.map((key) => [ `\`${key}\``, ``, ]), ]) } const colorKeys = [ "gray", "red", "pink", "purple", "cyan", "blue", "teal", "green", "yellow", "orange", ] export function getColorTokenDoc() { const colors = tokens.categoryMap.get("colors")! const allColors = Array.from(colors.values()).filter( (token) => colorKeys.some((key) => token.name.startsWith(`colors.${key}`)) && !token.extensions.conditions, ) return markdownTable([ ["Color Token", "Value", "Example"], ...allColors.map((token) => { const name = token.name.replace("colors.", "") return [ `\`${name}\``, `\`${token.value}\``, ``, ] }), ]) } export function getColorSemanticTokenDoc() { const colors = tokens.categoryMap.get("colors")! const allColors = Array.from(colors.values()) const bgTokens = markdownTable([ ["Background Token", "Example"], ...allColors .filter((token) => token.name.startsWith("colors.bg")) .map((token) => [ `\`${token.extensions.prop}\``, ``, ]), ]) const textTokens = markdownTable([ ["Text Token", "Example"], ...allColors .filter((token) => token.name.startsWith("colors.fg")) .map((token) => [ `\`${token.extensions.prop}\``, ``, ]), ]) const borderTokens = markdownTable([ ["Border Token", "Example"], ...allColors .filter((token) => token.name.startsWith("colors.border")) .map((token) => [ `\`${token.extensions.prop}\``, ``, ]), ]) const docs = [ `### Background`, bgTokens, `### Text`, textTokens, `### Border`, borderTokens, ] return docs.join("\n\n") } export function getFontTokenDoc() { const fonts = tokens.categoryMap.get("fonts")! const allFonts = Array.from(fonts.values()) return markdownTable([ ["Font Token", "Example"], ...allFonts.map((token) => [ `\`${token.extensions.prop}\``, ``, ]), ]) } export function getFontSizeTokenDoc() { const fontSizes = tokens.categoryMap.get("fontSizes")! const allFontSizes = Array.from(fontSizes.values()) return markdownTable([ ["Font Size Token", "Value", "Example"], ...allFontSizes.map((token) => [ `\`${token.extensions.prop}\``, `\`${token.value}\``, ``, ]), ]) } export function getFontWeightTokenDoc() { const fontWeights = tokens.categoryMap.get("fontWeights")! const allFontWeights = Array.from(fontWeights.values()) return markdownTable([ ["Font Weight Token", "Value", "Example"], ...allFontWeights.map((token) => [ `\`${token.extensions.prop}\``, `\`${token.value}\``, ``, ]), ]) } export function getLineHeightTokenDoc() { const lineHeights = tokens.categoryMap.get("lineHeights")! const allLineHeights = Array.from(lineHeights.values()) return markdownTable([ ["Line Height Token", "Value", "Example"], ...allLineHeights.map((token) => [ `\`${token.extensions.prop}\``, `\`${token.value}\``, ``, ]), ]) } export function getLetterSpacingTokenDoc() { const letterSpacings = tokens.categoryMap.get("letterSpacings")! const allLetterSpacings = Array.from(letterSpacings.values()) return markdownTable([ ["Letter Spacing Token", "Value", "Example"], ...allLetterSpacings.map((token) => [ `\`${token.extensions.prop}\``, `\`${token.value}\``, ``, ]), ]) } export function getZIndexTokenDoc() { const zIndices = tokens.categoryMap.get("zIndex")! const allZIndex = Array.from(zIndices.values()).sort( (a, b) => parseFloat(a.originalValue) - parseFloat(b.originalValue), ) return markdownTable([ ["Z Index Token", "Value", "Example"], ...allZIndex.map((token) => [ `\`${token.extensions.prop}\``, `\`${token.originalValue}\``, ``, ]), ]) } export function getBorderRadiusTokenDoc() { const borderRadius = tokens.categoryMap.get("radii")! const allBorderRadius = Array.from(borderRadius.values()) return markdownTable([ ["Border Radius Token", "Value", "Example"], ...allBorderRadius.map((token) => [ `\`${token.extensions.prop}\``, `\`${token.originalValue}\``, ``, ]), ]) } export function getShadowTokenDoc() { const shadows = tokens.categoryMap.get("shadows")! const allShadows = Array.from(shadows.values()) return markdownTable([ ["Shadow Token", "Example"], ...allShadows.map((token) => [ `\`${token.extensions.prop}\``, ``, ]), ]) } export function getSpacingTokenDoc() { const spacings = tokens.categoryMap.get("spacing")! const allSpacings = Array.from(spacings.values()) .filter( (token) => token.extensions.category === "spacing" && !token.extensions.negative, ) .sort((a, b) => parseFloat(a.value) - parseFloat(b.value)) return markdownTable([ ["Spacing Token", "Value", "Example"], ...allSpacings.map((token) => [ `\`${token.extensions.prop}\``, `\`${token.value}\``, ``, ]), ]) } export function replaceTokenDoc(text: string) { return text .replace("", getKeyframesDoc()) .replace("", getDurationsDoc()) .replace("", getAspectRatiosDoc()) .replace("", getBreakpointsDoc()) .replace("", getColorTokenDoc()) .replace("", getColorSemanticTokenDoc()) .replace("", getCursorDoc()) .replace("", getFontTokenDoc()) .replace("", getFontSizeTokenDoc()) .replace("", getFontWeightTokenDoc()) .replace("", getLineHeightTokenDoc()) .replace("", getLetterSpacingTokenDoc()) .replace("", getZIndexTokenDoc()) .replace("", getBorderRadiusTokenDoc()) .replace("", getShadowTokenDoc()) .replace("", getSpacingTokenDoc()) }