# Import Code [中文文档 (Chinese)](README.zh-CN.md) Embed local code files or files from remote repositories directly into your Obsidian notes, with syntax highlighting, symbol and line-range extraction, line highlighting, and zero-config references to multi-platform remote sources (GitHub, GitLab, Gitea, WebDAV, Generic URL). ## Features ### Embedding & Rendering - **Code embeds** - `![[file.ext]]` embeds a local code file; `![[alias:path/file.ext]]` embeds a remote one - **Symbol extraction** - `![[file.ext@functionName]]` extracts a specific function/class/method (supports brace languages like JS/TS/Java/C/Go/Rust, and indentation languages like Python/Ruby/YAML) - **Line range extraction** - `![[file.ext@10-30]]` extracts a line range; `@5` extracts a single line - **Line highlighting** - `![[file.ext#5-10]]` highlights specific lines in the rendered result; combinable with `@` - **HTML element extraction** - `![[file.html@.container]]` extracts an HTML element via a CSS selector - **Syntax highlighting** - language auto-detected from file extension, rendered through Obsidian's built-in MarkdownRenderer - **Line numbers** - optionally show real source-file line numbers beside the code block (offset-aware for `@` extraction) - **Code folding** - blocks exceeding a threshold fold automatically, scrollable while collapsed, preview lines configurable - **Long line wrapping** - optionally wrap overly long lines (default: horizontal scroll) - **Loading skeleton** - pulsing placeholder while remote/alias content is fetched ### Remote & Upload - **Remote source aliases** - pre-configure remote services in settings (GitHub / GitLab / Gitea / WebDAV / Generic URL / Local Directory) and reference them by alias instead of typing full URLs every time - **Upload sources** - when creating a code file from the clipboard, upload it to Local vault / WebDAV / GitHub Gist with an embed link generated automatically - **SSL skip verification** - supports HTTPS servers with self-signed/expired certificates (desktop only), with startup diagnostics and graceful degradation ### Interaction - **Toolbar** - each embed block has "open source file" and "copy code" buttons (with language tag) in the top-right corner; oversized blocks also get an expand/collapse button - **Dual mode support** - works in both Reading view (MarkdownPostProcessor) and Live Preview (CodeMirror ViewPlugin) - **Auto refresh** - local file changes trigger re-render automatically (300ms debounce) - **Insert code file** - create a code file from clipboard content and insert an embed link, with automatic language detection - **Re-reference** - quickly re-reference the last inserted code file, with adjustable extraction and highlight ranges ## Installation ### Manual installation 1. Download `main.js`, `manifest.json`, and `styles.css` from the latest Release 2. Put them in `/.obsidian/plugins/import-code/` 3. Reload Obsidian and enable the plugin in **Settings -> Community plugins** ### Build from source ```bash git clone https://github.com/yu4n2h1/obsidian-import-code.git cd obsidian-import-code yarn install yarn build ``` ## Usage ### Basic embeds ```markdown ![[src/utils.ts]] ![[config.json]] ![[scripts/deploy.sh]] ``` ### Symbol extraction (`@`) ```markdown ![[src/utils.ts@parseConfig]] ![[models/user.py@UserClass]] ![[lib/helper.rs@process_data]] ``` ### Line ranges (`@` with numbers) ```markdown ![[src/utils.ts@10-30]] ![[data/schema.sql@5]] ``` ### Line highlighting (`#`) ```markdown ![[src/app.ts#5-10]] ![[src/main.ts@init#5-12]] ``` ### HTML element extraction (`@` with a CSS selector) For `.html` files, `@` accepts a CSS selector to extract a specific element: ```markdown ![[template.html@.container]] ![[page.html@[id="main"]]] ![[layout.html@div.content]] ![[index.html@section > p]] ``` | Selector example | Extracted content | |------------------|-------------------| | `.container` | the element with `class="container"` | | `[id="main"]` | the element with `id="main"` | | `div.content` | the `
` | | `nav > a` | `` elements directly under `