Skip to content

anbeime/skill / skills/content-creation-publisher/baoyu-format-markdown

baoyu-format-markdown

Unverified

技术写作员与内容创作者在处理纯文本或 Markdown 稿件时,若需优化文章结构、添加 frontmatter 或自动修正中英文排版间距,使用此技能可一键生成专业美观的 {filename}-formatted.md 文件,让文档排版更完美。

FileKindSize
SKILL.mdskill md11.4 KB
scripts/autocorrect.tsscript246 B
scripts/bun.lockother20.8 KB
scripts/main.tsscript4.7 KB
scripts/package-lock.jsonmanifest31.1 KB
scripts/package.jsonmanifest275 B
scripts/quotes.tsscript173 B

scripts/main.ts

View on GitHub
import { readFileSync, writeFileSync } from "fs";
import { unified } from "unified";
import remarkParse from "remark-parse";
import remarkCjkFriendly from "remark-cjk-friendly";
import remarkGfm from "remark-gfm";
import remarkFrontmatter from "remark-frontmatter";
import remarkStringify from "remark-stringify";
import { visit } from "unist-util-visit";
import YAML from "yaml";
import { replaceQuotes } from "./quotes";
import { applyAutocorrect } from "./autocorrect";

export interface FormatOptions {
  quotes?: boolean;
  spacing?: boolean;
  emphasis?: boolean;
}

export interface FormatResult {
  success: boolean;
  filePath: string;
  quotesFixed: boolean;
  spacingApplied: boolean;
  emphasisFixed: boolean;
  error?: string;
}

const DEFAULT_OPTIONS: Required<FormatOptions> = {
  quotes: false,
  spacing: true,
  emphasis: true,
};

function decodeHtmlEntities(text: string): string {
  return text.replace(/&#x([0-9A-Fa-f]+);/g, (_, hex) =>
    String.fromCodePoint(parseInt(hex, 16))
  );
}

function formatFrontmatter(value: string): string | null {
  try {
    const doc = YAML.parseDocument(value);
    return doc.toString({ lineWidth: 0 }).trimEnd();
  } catch {
    return null;
  }
}

function formatMarkdownContent(
  content: string,
  options: Required<FormatOptions>
): string {
  const processor = unified()
    .use(remarkParse)
    .use(options.emphasis ? remarkCjkFriendly : [])
    .use(remarkGfm)
    .use(remarkFrontmatter, ["yaml"])
    .use(remarkStringify, {
      wrap: false,
    });

  const tree = processor.parse(content);

  visit(tree, (node) => {
    if (node.type === "text" && options.quotes) {
      const textNode = node as { value: string };
      textNode.value = replaceQuotes(textNode.value);
      return;
    }
    if (node.type === "yaml") {
      const yamlNode = node as { value: string };
      const formatted = formatFrontmatter(yamlNode.value);
      if (formatted !== null) {
        yamlNode.value = formatted;
      }
      return;
    }
  });

  let result = processor.stringify(tree);
  if (options.emphasis) {
    result = decodeHtmlEntities(result);
  }
  return result;
}

export function formatMarkdown(
  filePath: string,
  options?: FormatOptions
): FormatResult {
  const opts: Required<FormatOptions> = { ...DEFAULT_OPTIONS, ...options };

  const result: FormatResult = {
    success: false,
    filePath,
    quotesFixed: false,
    spacingApplied: false,
    emphasisFixed: false,
  };

  try {
    const content = readFileSync(filePath, "utf-8");
    const formattedContent = formatMarkdownContent(content, opts);

    result.quotesFixed = opts.quotes;
    result.emphasisFixed = opts.emphasis;

    writeFileSync(filePath, formattedContent, "utf-8");

    if (opts.spacing) {
      result.spacingApplied = applyAutocorrect(filePath);
    }

    result.success = true;
    console.log(`✓ Formatted: ${filePath}`);
  } catch (error) {
    result.error = error instanceof Error ? error.message : String(error);
    console.error(`✗ Format failed: ${result.error}`);
  }

  return result;
}

function parseArgs(args: string[]): { filePath: string; options: FormatOptions } {
  const options: FormatOptions = {};
  let filePath = "";

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

    if (arg === "--quotes" || arg === "-q") {
      options.quotes = true;
    } else if (arg === "--no-quotes") {
      options.quotes = false;
    } else if (arg === "--spacing" || arg === "-s") {
      options.spacing = true;
    } else if (arg === "--no-spacing") {
      options.spacing = false;
    } else if (arg === "--emphasis" || arg === "-e") {
      options.emphasis = true;
    } else if (arg === "--no-emphasis") {
      options.emphasis = false;
    } else if (arg === "--help" || arg === "-h") {
      console.log(`Usage: npx -y bun scripts/main.ts <file.md> [options]

Options:
  -q, --quotes       Replace ASCII quotes with fullwidth quotes (default: false)
      --no-quotes    Do not replace quotes
  -s, --spacing      Add CJK/English spacing via autocorrect (default: true)
      --no-spacing   Do not add CJK/English spacing
  -e, --emphasis     Fix CJK emphasis punctuation issues (default: true)
      --no-emphasis  Do not fix CJK emphasis issues
  -h, --help         Show this help message`);
      process.exit(0);
    } else if (!arg.startsWith("-")) {
      filePath = arg;
    }
  }

  return { filePath, options };
}

if (import.meta.url === `file://${process.argv[1]}`) {
  const { filePath, options } = parseArgs(process.argv.slice(2));

  if (!filePath) {
    console.error("Usage: npx -y bun scripts/main.ts <file.md> [options]");
    console.error("Use --help for more information.");
    process.exit(1);
  }

  const result = formatMarkdown(filePath, options);
  if (!result.success) {
    process.exit(1);
  }
}
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 197kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall