@ape-egg/codie

中文文档
0.2.4发布于 2026/11/01ISCES Module零依赖近 30 天下载 2704

Modular code display and editing package

原始使用文档
文档目录26 节
  1. Overview
  2. Features
  3. Core Features
  4. Editing Features
  5. Installation
  6. Usage
  7. Basic Syntax Highlighting
  8. HTML Inside the Block
  9. Editable Code with Line Numbers
  10. Live Preview with onEdit
  11. Diff Mode
  12. Language-Specific Highlighting
  13. API
  14. codie(selector | element | config)
  15. Instance Properties
  16. Exported Utilities
  17. Attributes
  18. Container Attributes
  19. Runtime Attributes (applied by codie)
  20. Context Variables
  21. Architecture
  22. Vibe Integration
  23. Roadmap
  24. Implemented
  25. Planned
  26. License

Codie

Modular code display and editing package for the web.

Overview

Codie is a lightweight, modular code highlighter and editor that bridges the gap between static code display and full in-browser IDEs. Built with vanilla JavaScript, it provides syntax highlighting, live editing, and seamless integration with reactive frameworks like Vibe.

Vision: Codie is a spectrum from static highlighter to full in-browser IDE:

Minimal (vanilla)     →     Editor     →     Full REPL (vibe-powered)
highlight + format    +editable             +live output, inspect,
                      +numberedRows          visual editing, zero-build HMR

Features

Core Features

  • Syntax Highlighting: HTML, CSS, JavaScript, JSON, shell and Python, detected from the code itself; YAML (yaml/yml) and Markdown (markdown/md, fenced blocks drawn in their own language) when named. Any other language draws as escaped plain text
  • Editable Mode: Transform static code into a live editor with textarea overlay
  • Line Numbers: Optional numbered rows for code reference
  • Diff Mode: Diff a before text against the code, drawn with the same highlighter
  • Its Own Element: <codie> is the block; <template codie> holds HTML the browser must not parse
  • Language Modes: Pin a language via attributes (highlight-html, highlight-css, highlight-js, highlight-json)
  • Dark/Light Themes: Toggle with [dark] attribute
  • Runtime State Toggle: Change features dynamically via codieRef.editable, codieRef.numberedRows

Editing Features

  • Tab Handling: Tab key inserts 2 spaces
  • Multi-line Indent: Select multiple lines + Tab indents all
  • Outdent: Shift+Tab removes indentation
  • Live Preview: onEdit callback fires on every change
  • Error Handling: onError callback for execution failures

Installation

npm install @ape-egg/codie

Usage

Basic Syntax Highlighting

<link rel="stylesheet" href="@ape-egg/codie/codie.css" />
<script type="module">
  import codie from '@ape-egg/codie';

  codie();
</script>

<codie>
  const greet = (name) => `Hello, ${name}!`;
</codie>

codie() with no argument takes the first <codie> or [codie] element in the document.

HTML Inside the Block

The browser parses markup written inside <codie>. Put HTML in a <template codie> instead, so it reaches codie as text:

<template codie>
  <div>
    <p>Hello, world!</p>
  </div>
</template>

The codie attribute makes any element a codie instance; <codie> needs none.

Editable Code with Line Numbers

import codie from '@ape-egg/codie';

const editor = codie('codie');
editor.editable = true;
editor.numberedRows = true;

Live Preview with onEdit

const outputRef = document.querySelector('output');

const editor = codie('codie');
editor.editable = true;
editor.onEdit = ({ formatted, raw }) => {
  outputRef.innerHTML = formatted;
};

Diff Mode

const view = codie({ el, diff: true, before: previous });

view.code = draft; // redraws the diff
view.before = other; // redraws the diff
view.diff = false; // back to a plain view of the same text
view.editable = true; // edit the after text; removed rows hide, added rows follow

Both texts are highlighted whole by codie's own highlighter, then split into rows. Changed rows come in runs, each run's added block above its removed block. A removed and an added line are paired as the same line edited when at least half their words (punctuation not counted) survive, and only then are their changed words marked. With numberedRows the rows carry their before and after line numbers. The whole diff renders in one go, like the plain view.

diff(before, after, language?, { more }?) from @ape-egg/codie/diff returns the rows as HTML for use outside an instance. Given more, unchanged lines further than three from a change fold into one full-width .codie-diff-fold row that reads ... 12 more lines ... with more as its words. The row is a <details> holding the lines it stands for, so a click opens them in place with no script.

Language-Specific Highlighting

<!-- HTML, with its <script>, <style> and on* handlers -->
<template codie highlight-html>...</template>

<!-- CSS: stylesheets or bare declaration lists -->
<codie highlight-css>...</codie>

<!-- JavaScript -->
<codie highlight-js>...</codie>

<!-- JSON -->
<codie highlight-json>...</codie>

<!-- Detected from the code: HTML, CSS, JavaScript, JSON, shell, Python or plain text (default) -->
<codie>...</codie>

A language set on the instance (codieRef.language = 'yaml') or passed to highlight(code, language) / diff(before, after, language) can be any of html, css, js, json, python, shell, yaml, yml, markdown, md or text. A language codie has no highlighter for, such as toml, draws as escaped plain text.

API

codie(selector | element | config)

Initialize a codie instance.

Parameters:

  • selector (string): Query selector for the codie element — codie for the element, [codie] for an attributed one
  • element (Element): DOM element reference
  • config (object): Configuration object with optional el, editable, numberedRows, foldable, diff and before properties; without el the element is the first codie, [codie] in the document

Returns: Codie instance proxy

Instance Properties

const editor = codie('codie');

// Get/set code content
editor.code; // Get current code
editor.code = '<div>New code</div>'; // Set code (updates display + textarea)

// Toggle features
editor.editable = true; // Enable editing
editor.numberedRows = true; // Show line numbers
editor.foldable = false; // Disable code folding (experimental)
editor.diff = true; // Diff `before` against the code
editor.before = 'old code'; // Set the before text (redraws when diffing)

// Callbacks
editor.onEdit = ({ formatted, raw }) => {
  // formatted: cleaned up for display
  // raw: exact code content
};

editor.onError = (error) => {
  // Handle errors during editing
};

Exported Utilities

import {
  highlightHTML,
  highlightJS,
  highlightCSS,
  highlightJSRaw,
  escapeHTML,
  unescapeHTML,
  formatDocument,
  normalizeInlineWhitespace,
  ATTR,
  CLASS,
} from '@ape-egg/codie';

Attributes

Container Attributes

A <codie> element is an instance by its tag. The attributes below go on it, or on any other element carrying codie:

  • codie - Marks an element that is not <codie> as an instance, <template codie> above all
  • dehydrate - Skip initialization (for documentation)
  • highlight-html - Highlight as HTML, including embedded <script>, <style> and on* handlers
  • highlight-css - Highlight as CSS
  • highlight-js - Highlight as JavaScript
  • highlight-json - Highlight as JSON
  • none of the above - Detect the language from the code (HTML, CSS, JavaScript, JSON, shell, Python or plain text)
  • dark - Apply dark theme

Runtime Attributes (applied by codie)

  • codie-editable - Applied when editable mode is active
  • codie-numbered - Applied when line numbers are shown
  • codie-foldable - Applied when folding is enabled (experimental)
  • diff - Applied when diff mode is on

Context Variables

Codie sizes itself from CSS custom properties set on a parent, so it lines up with whatever layout system surrounds it. Each one has a fallback:

  • --unit (4px) - Spacing unit; line height is 6 units, padding 4 units
  • --font-mono (Geist Mono stack) - Font family

Architecture

Codie uses a layered approach with CSS Grid:

  1. Display Layer (<pre>) - Syntax-highlighted code
  2. Textarea Layer (optional) - Editable overlay when editable = true
  3. Line Numbers (optional) - Positioned overlay when numberedRows = true

All layers are synchronized via a reactive proxy that updates the DOM when properties change.

Vibe Integration

Codie pairs naturally with Vibe for live, reactive code editing:

import { init } from '@ape-egg/vibe';
import codie from '@ape-egg/codie';

init({ output: '' });

const editor = codie('template[codie]');
editor.editable = true;
editor.onEdit = ({ formatted }) => {
  $.output = formatted;
};
<template codie>
  <p>Edit me!</p>
</template>

<output>@[output]</output>

Roadmap

Implemented

  • ✓ Syntax highlighting (HTML, CSS, JS, JSON, shell, Python, YAML, Markdown) with language detection and a plain-text fallback
  • ✓ Editable mode with textarea overlay
  • ✓ Line numbers
  • ✓ Tab handling (insert spaces, multi-line indent, outdent)
  • ✓ onEdit/onError callbacks
  • ✓ Template support
  • ✓ Language modes
  • ✓ Dark/light themes
  • ✓ Diff mode

Planned

  • Self-documenting REPL (execute code from editor)
  • Smart script execution (only re-run when changed)
  • Vibe exclusion zones (prevent parent vibe from watching preview)
  • Hot state preservation (preserve state across re-execution)
  • Code folding (deferred due to complexity with editable mode)
  • Inspect mode (click preview element to highlight code)
  • Visual editing (drag-drop reorder in preview)

License

MIT

版本与发布记录

@ape-egg/codie 共发布 23 个版本,首次发布于 2026/03/05;过去 12 个月发布了 23 个版本。

版本发布日期距今
0.2.4latest2026/11/016 天前
0.2.32026/10/299 天前
0.2.12026/10/1325 天前
0.2.02026/10/1127 天前
0.1.182026/10/1028 天前
0.1.172026/10/0929 天前

关于 @ape-egg/codie 的常见问题

@ape-egg/codie 是什么?

Modular code display and editing package

如何安装 @ape-egg/codie?

在项目目录运行 npm install @ape-egg/codie,或使用 pnpm add / yarn add / bun add。

@ape-egg/codie 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/ape-egg__codie 是否存在。

@ape-egg/codie 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@ape-egg/codie 可以商用吗?

@ape-egg/codie 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ape-egg/codie 的下载量有多少?

根据 npm 官方下载统计,@ape-egg/codie 在 2026/10/06 – 2026/11/04 这 30 天内被下载 2,704 次,日均约 90 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ape-egg/codie 还在维护吗?

最新版本 0.2.4 发布于 2026/11/01(6 天前);至今共发布 23 个版本,其中过去 12 个月发布了 23 个;首次发布于 2026/03/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。