Skip to content

Auto complete

Orange46707 Eery edited this page Jun 21, 2026 · 2 revisions

Auto-complete 模块

概述

Auto-complete 模块是从 NetLeaf 主库分离出来的独立库,用于自动补全内联HTML/Vue代码中缺失的编码声明,以及自动引入Vue库。

版本: 与主库共用版本号

平台支持:

  • ✅ Windows
  • ✅ Linux
  • ✅ macOS

功能特性

1. Charset 自动补全

自动检测HTML中是否缺少 <meta charset> 和 <meta name="viewport"> 标签,如果缺少则自动添加。

  • 根据程序设置的统一编码格式补全charset
  • 智能识别标签位置,在合适的地方插入

2. Vue 自动引入

检测HTML中是否包含Vue代码但没有引入Vue库,如果是则自动添加CDN引用。

  • 支持多种CDN:unpkg、cdnjs、jsdelivr、本地文件
  • 支持指定Vue版本
  • 智能检测:需要检测到至少2种Vue模式才会添加引用

3. 灵活启用方式

支持多种启用/禁用方式:

  • 整数: 1 (启用), 0 (禁用)
  • 字符串: "true", "false", "on", "off", "yes", "no" (大小写不敏感)

4. 独立功能控制

charset和Vue功能可以单独启用或禁用:

nl_autocomplete_enable_feature(NL_AUTOCOMPLETE_FEATURE_CHARSET);  // 启用charset
nl_autocomplete_disable_feature(NL_AUTOCOMPLETE_FEATURE_VUE);     // 禁用Vue

API 参考

初始化与配置

函数 说明
nl_autocomplete_init() 初始化模块
nl_autocomplete_is_available() 检查模块是否可用
nl_autocomplete_version() 获取模块版本

启用/禁用

函数 说明
nl_autocomplete_enable(int) 启用/禁用模块(整数)
nl_autocomplete_enable_ex(const char*) 启用/禁用模块(字符串)
nl_autocomplete_is_enabled() 检查模块是否启用

功能控制

函数 说明
nl_autocomplete_enable_feature(nl_autocomplete_feature_t) 启用指定功能
nl_autocomplete_disable_feature(nl_autocomplete_feature_t) 禁用指定功能
nl_autocomplete_enable_feature_ex(nl_autocomplete_feature_t, const char*) 字符串方式启用/禁用
nl_autocomplete_is_feature_enabled(nl_autocomplete_feature_t) 检查功能是否启用

编码设置

函数 说明
nl_autocomplete_set_encoding(const char*) 设置默认编码
nl_autocomplete_get_encoding() 获取当前编码

Charset 补全

函数 说明
nl_charset_complete_create() 创建charset补全上下文
nl_charset_complete_process() 处理HTML并补全charset
nl_charset_complete_destroy() 销毁上下文
nl_complete_charset() 快速补全charset

Vue 引入

函数 说明
nl_vue_import_create() 创建Vue引入上下文
nl_vue_import_process() 处理HTML并添加Vue引用
nl_vue_import_process_version() 指定版本处理
nl_vue_import_destroy() 销毁上下文
nl_import_vue() 快速添加Vue引用
nl_html_has_vue_code() 检测是否包含Vue代码
nl_html_has_vue_import() 检测是否已有Vue引用

综合处理

函数 说明
nl_autocomplete_process_html() 使用所有启用功能处理HTML

使用示例

#include "netleaf_autocomplete.h"

int main() {
    // 初始化模块
    nl_autocomplete_init();
    
    // 设置统一编码格式
    nl_autocomplete_set_encoding("UTF-8");
    
    // 启用模块(支持字符串)
    nl_autocomplete_enable_ex("true");
    
    // 单独控制功能
    nl_autocomplete_enable_feature(NL_AUTOCOMPLETE_FEATURE_CHARSET);
    nl_autocomplete_disable_feature(NL_AUTOCOMPLETE_FEATURE_VUE);
    
    // 处理HTML
    const char* html = "<html><body>Hello</body></html>";
    char* result = nl_autocomplete_process_html(html, strlen(html), NULL);
    
    // 输出: <html><head><meta charset="UTF-8">...</head><body>Hello</body></html>
    
    free(result);
    return 0;
}

检测模式

Vue代码检测模式

模块会检测以下Vue代码模式:

  • new Vue(
  • Vue.createApp
  • createApp(
  • ref(
  • reactive(
  • computed(
  • watch(
  • watchEffect(
  • onMounted(
  • onUpdated(
  • onUnmounted(
  • v-if
  • v-for
  • v-show
  • v-bind
  • v-model
  • {{ 和 }}
  • :class
  • :style
  • @click
  • @submit
  • vue-app
  • id="app"

检测到至少2种模式才会认为是Vue代码。

Vue引用检测模式

模块会检测以下Vue引用模式:

  • vue.js
  • vue.min.js
  • vue.global.js
  • vue.runtime.js
  • cdn.jsdelivr.net/npm/vue
  • unpkg.com/vue@
  • cdnjs.cloudflare.com/ajax/libs/vue/
  • vuejs.org/

CDN类型

类型 URL模板
NL_VUE_CDN_UNPKG https://unpkg.com/vue@{version}/dist/vue.global.js
NL_VUE_CDN_CDNJS https://cdnjs.cloudflare.com/ajax/libs/vue/{version}/vue.min.js
NL_VUE_CDN_JSDELIVR https://cdn.jsdelivr.net/npm/vue@{version}/dist/vue.global.js
NL_VUE_CDN_LOCAL ./vue.global.js

编译选项

BUILD_AUTOCOMPLETE=ON   # 默认启用

注意事项

  1. 该模块是独立库,可以单独编译和加载
  2. 如果模块未加载,相关功能不可用,但不会影响主库运行
  3. 建议在程序启动时初始化模块
  4. 编码设置会影响所有charset补全操作

导航 | Navigation

快速链接 | Quick Links

开发参考 | Development

附加模块 | Modules

  • Auto-complete - 自动补全charset和Vue引入
  • Auto-route - 自动路由建议
  • ErrorPage - 自定义错误页面模板
  • IPC - 进程间通讯服务
  • LinkAgg - 同端口链路聚合
  • Lang - 多语言错误消息
  • Vue - Vue.js后端支持
  • Extension - 扩展库开发指南
  • TLS - TLS/SSL 加密扩展
  • MQTT - MQTT v5.0 协议客户端
  • MQTT-Server - MQTT v5.0 协议服务端(Broker)

关于 | About

作者: 508364

版本: 2.4.2

许可: MIT License


外部链接 | External Links

Clone this wiki locally