import{B as e,P as t,_ as n,f as r,g as i,h as a,l as o}from"./runtime-core.esm-bundler-CZvn3YaB.js";import{t as s}from"./plugin-vue_export-helper-CxTVcLa7.js";var c=JSON.parse(`{"path":"/guide/code/copy-code/","title":"代码复制 | 指南","lang":"zh-CN","frontmatter":{"title":"代码复制","icon":"ph:code","createTime":"2024/03/04 09:59:29","permalink":"/guide/code/copy-code/","description":"概述 该功能由 @vuepress/plugin-copy-code 提供支持。 主题默认启用了代码复制功能。支持 一键复制 在 文章中 展示的代码。 默认情况下, 主题会为每一个 代码块 添加一个复制按钮。该按钮仅在桌面端显示。 当鼠标悬停在 代码块 上时,在右上角会出现一个复制按钮。 配置 在 .vuepress/config.ts 配置文件中,修...","head":[["script",{"type":"application/ld+json"},"{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Article\\",\\"headline\\":\\"代码复制\\",\\"image\\":[\\"\\"],\\"dateModified\\":\\"2025-10-07T15:13:09.000Z\\",\\"author\\":[]}"],["meta",{"property":"og:url","content":"https://theme-plume.vuejs.press/guide/code/copy-code/"}],["meta",{"property":"og:site_name","content":"Plume 主题"}],["meta",{"property":"og:title","content":"代码复制"}],["meta",{"property":"og:description","content":"概述 该功能由 @vuepress/plugin-copy-code 提供支持。 主题默认启用了代码复制功能。支持 一键复制 在 文章中 展示的代码。 默认情况下, 主题会为每一个 代码块 添加一个复制按钮。该按钮仅在桌面端显示。 当鼠标悬停在 代码块 上时,在右上角会出现一个复制按钮。 配置 在 .vuepress/config.ts 配置文件中,修..."}],["meta",{"property":"og:type","content":"article"}],["meta",{"property":"og:locale","content":"zh-CN"}],["meta",{"property":"og:locale:alternate","content":"en-US"}],["meta",{"property":"og:updated_time","content":"2025-10-07T15:13:09.000Z"}],["meta",{"property":"article:modified_time","content":"2025-10-07T15:13:09.000Z"}],["link",{"rel":"alternate","hreflang":"en-us","href":"https://theme-plume.vuejs.press/en/guide/code/copy-code/"}]]},"readingTime":{"minutes":1.45,"words":434},"git":{"createdTime":1743474214000,"updatedTime":1759849989000,"contributors":[{"name":"pengzhanbo","username":"pengzhanbo","email":"volodymyr@foxmail.com","commits":2,"avatar":"https://avatars.githubusercontent.com/pengzhanbo?v=4","url":"https://github.com/pengzhanbo"}],"changelog":[{"hash":"4d2361a7046214fe0f4e4af01831107fd00e38ad","time":1759849989000,"email":"volodymyr@foxmail.com","author":"pengzhanbo","message":"feat(theme)!: add collections support (#704)"},{"hash":"74dfa34ceed33c3517fdf53abbc8897b95b3f94c","time":1743474214000,"email":"volodymyr@foxmail.com","author":"pengzhanbo","message":"docs: enable twoslash for code block"}]},"autoDesc":true,"filePathRelative":"guide/code/copy-code.md","headers":[],"bulletin":false}`),l={name:`copy-code.md`},u={class:`code-block-title`,"data-title":`.vuepress/config.ts`},d={class:`code-block-title-bar`},f={class:`title`},p={id:`transform`,tabindex:`-1`},m={class:`header-anchor`,href:`#transform`},h={class:`code-block-title`,"data-title":`.vuepress/client.ts`},g={class:`code-block-title-bar`},_={class:`title`},v={class:`code-block-title`,"data-title":`.vuepress/client.ts`},y={class:`code-block-title-bar`},b={class:`title`};function x(s,c,l,x,S,C){let w=e(`VPIcon`),T=e(`Badge`);return t(),r(`div`,null,[c[7]||=a(`

概述

该功能由 @vuepress/plugin-copy-code 提供支持。

主题默认启用了代码复制功能。支持 一键复制 在 文章中 展示的代码。

默认情况下, 主题会为每一个 代码块 添加一个复制按钮。该按钮仅在桌面端显示。

当鼠标悬停在 代码块 上时,在右上角会出现一个复制按钮。

配置

.vuepress/config.ts 配置文件中,修改 代码复制的行为:

`,7),o(`div`,u,[o(`div`,d,[o(`span`,f,[n(w,{provider:`iconify`,name:`vscode-icons:file-type-typescript`}),c[0]||=i(`.vuepress/config.ts`,-1)])]),c[1]||=a(`
export default defineUserConfig({
  theme: plumeTheme({
    // copyCode: false  // 禁用代码复制
    copyCode: {
      // ...更多配置
    }
  })
})
`,1)]),c[8]||=a(`

showInMobile

是否在移动端显示复制按钮。

ignoreSelector

代码块中的元素选择器,用于在复制时忽略相关元素。

例如: ['.token.comment'] 将忽略代码块中类名为 .token.comment 的节点 (这会在 prismjs 中忽略注释)。

inlineSelector

是否在双击时复制行内代码内容。

boolean: 是否在双击时复制行内代码内容。

string[] | string: 选择器,表示需要复制的行内代码内容。

`,12),o(`h3`,p,[o(`a`,m,[o(`span`,null,[c[2]||=i(`transform `,-1),n(T,{type:`tip`,text:`仅限组合式 API`})])])]),c[9]||=a(`

一个转换器,用于在复制之前对 <pre> 中代码块内容进行修改。该选项仅在使用 useCopyCode() 时有效。

组合式 API

该功能的组合式 API 可以在 .vuepress/client.ts 中配置:

`,4),o(`div`,h,[o(`div`,g,[o(`span`,_,[n(w,{provider:`iconify`,name:`vscode-icons:file-type-typescript`}),c[3]||=i(`.vuepress/client.ts`,-1)])]),c[4]||=a(`
import { defineClientConfig } from '@vuepress/client'
import { useCopyCode } from '@vuepress/plugin-copy-code/client'

export default defineClientConfig({
  setup() {
    useCopyCode({
      // ...
    })
  },
})
`,1)]),c[10]||=o(`h3`,{id:`示例`,tabindex:`-1`},[o(`a`,{class:`header-anchor`,href:`#示例`},[o(`span`,null,`示例`)])],-1),c[11]||=o(`p`,null,`代码复制时,添加 copyright 信息`,-1),o(`div`,v,[o(`div`,y,[o(`span`,b,[n(w,{provider:`iconify`,name:`vscode-icons:file-type-typescript`}),c[5]||=i(`.vuepress/client.ts`,-1)])]),c[6]||=a(`
import { defineClientConfig } from '@vuepress/client'
import { useCopyCode } from '@vuepress/plugin-copy-code/client'

export default defineClientConfig({
  setup() {
    useCopyCode({
      transform: (preElement) => {
        // 插入版权信息
        pre.innerHTML += \`\\n Copied by vuepress-theme-plume\`
      },
    })
  },
})
`,1)])])}var S=s(l,[[`render`,x]]);export{c as _pageData,S as default};