跳转到主要内容

复用页面内容

在 BookStack 中,你可能希望将同一块内容放在多个位置。你可以将该内容复制粘贴到多个页面中,但这样一来,如果需要修改该内容,就必须在不同页面中逐一更新。通过使用包含标签,你可以包含其他页面的全部内容,也可以仅包含其他页面中的单个内容块。

注意事项

通过包含标签复用页面内容时,需要考虑以下事项:

  • 包含操作会执行权限校验,因此只有当前用户有权查看的页面,其内容才会被包含。在查看页面时,包含内容并不明显,也不透明,因此用户可能在未考虑所用权限控制的情况下,将含有受权限控制内容的页面分享给无权限的用户。
  • 包含标签最多支持三级嵌套,此限制用于确保系统性能并避免递归问题。
  • 被包含的内容不会作为可搜索内容索引到其被包含的页面中。
  • 被包含的内容不会显示在内容预览文本或摘要中。

包含标签

若要在另一个页面中包含某个页面的内容,可以使用以下语法:

{{@page_id}} 或 {{@page_id#content_id}}

以下是一些使用示例:

  • Within BookStack you can attach files & links to pages, so they can be referenced within content and shown available for download. Access to attachments is controlled via view permissions to the page they're uploaded to, so users can only access attachments for pages they can view in the system.

    File attachments are really just there for storage and access convenience. BookStack does not do anything special with attachments like scan & index their contents, since documentation content in BookStack is designed & intended to be within the text of pages.


    Creating & Uploading Attachments

    Attachments can be uploaded via the sidebar when in the page editor. The attachments section of the sidebar is represented with a paper-clip icon and, when open, looks like this:

    View of the BookStack page editor, with the sidebar open on an "Attachments" view, with the buttons "Upload file" and "Attach Link", along with two listed attachment boxes below labelled as a link with names "kitty-cat.jpg" and "Secret Location"

    With this open there are options to "Upload File" or "Attach Link". You can also just drag and drop a file into that zone around the buttons to quickly upload a file from your device. Uploading files is the most common use-case but you can attach a link to the page to act as a dynamic, permission-controlled, reference/URL you can use & share.

    Below the buttons is where any existing uploaded attachments can be found. You can use the handles on the left to reorder them if needed. There are buttons on each to delete or edit the attachment. Via editing you can alter the attachment name in addition to the underlying file or link, while the original attachment URL will remain the same. There's also a button to insert the attachment into the page, as referenced in the "Using Attachments in Pages" section below.

    Keep in mind, attachments are strongly coupled to the pages they're uploaded to. Access to an attachment is controlled via the ability to see the owning page. If a page is deleted, the attachments uploaded to it will be deleted too!


    Accessing Attachments

    One uploaded, attachments will show in the sidebar when viewing a page. You can click on an attachment to open it up. Link attachments will open in a new tab. File attachments will download, or you can view them in a new tab via the dropdown menu on the right of the attachment:

    View of the sidebar when viewing a page with two attachments: a "kitty-cat.jpg" file attachment and "Secret Location" link attachment. There's also a view of the same but showing a dropdown menu on the right of the attachment, with "Download" and "Open in Tab" options

    Alternatively you can hold down Ctrl (or Cmd on Mac) and then click on it directly to open in a new tab. Note that attachments in new tabs may still download if the file format is not deemed safe to show via a new tab, or if that's the browser's default behaviour.


    Using Attachments in Pages

    While attachments are listed in the sidebar when viewing a page, as shown in the section above, you can link to attachments within the page itself which can be a common need when wanting to add context or reference the attachment in a particular part of your documentation content. There are a few ways to do this.

    The first method is to simply use the "link" icon button on the attachment in the sidebar. Clicking this will insert a link into the page, at the current cursor position, with the correct attachment URL & name.

    The second method is via drag & drop. You can drag the attachment from the sidebar using its handle or name, and then drop it into the editor which will insert an attachment link at that location.

    View of the page editor, with the attachments sidebar open. An attachment card named "Secret Location" is being dragged into the body of the page editor.

    The third is just using normal browser and editor functionality. You can right-click and copy the URL for the attachment, then insert a link as normal into the page using that attachment URL you just copied.


    Technical Details

    When you upload & download attachments, BookStack will attempt to stream files where possible so that the application can serve large files quickly without needing to use up system memory or increase the PHP memory limit. If your instance is using S3-based storage, then attachments are streamed from BookStack to server but not from S3 (or similar) to BookStack at this time, so it may be better to store and serve attachments direct from storage if possible in those large-file circumstances.

    Even with streaming, you can often run into server-side (and proxy) upload/download/timeout limits. Our admin documentation on file uploads may help if you run into these.

    When attachments are served inline (opened in new tab or with an open=true query parameter) BookStack will attempt to sniff the mime-type of the attachment content, then serve the file with that mime-type to allow in-browser viewing if supported, and if the detected mime-type is deemed safe (For example, HTML won't be served inline). This is done via an internal allow-list of mime-type values.

    - 包含 id 为 '5' 的页面的全部内容。
  • - 包含 id 为 '10' 的页面中 id 为 bkmrk-copyright-year 的元素内的内容。

只需在页面编辑器中输入相应内容,查看页面时便会动态获取内容。这意味着你可以更新被引用的内容,所有包含该内容的页面都会同步显示相同的更改。

快速获取语法

查找页面的 id 可能比较困难,因此 BookStack 内置了一种快速简便的方式来查找包含标签。 查看包含所需内容的页面。使用鼠标选中部分内容,此时会弹出一个窗口。 默认情况下,其中包含指向该内容的直接链接。如果点击左侧的链接图标,则会切换为显示该内容的包含标签。复制该标签并粘贴到另一个页面中即可。移除 # 和内容 id,即可包含整个页面的内容,而不只是单个内容块。

指定内容 ID

包含特定内容块时,将使用该内容块的 id(即哈希符号之后的部分)。默认情况下,这些 id 由 BookStack 在保存页面时生成。如果使用 WYSIWYG 编辑器,除非该内容块被删除或重新格式化,否则 id 会保持不变。在 Markdown 编辑器中,id 会根据内容在每次保存时发生变化,因此虽然它可能保持不变,但 id 仍有较大可能发生改变,所以如果要将该内容用于包含,建议手动指定 ID。

注意:所有 ID 值必须以 "bkmrk" 开头,以避免被覆盖。

WYSIWYG 编辑器

在 WYSIWYG 编辑器中,可以进入源代码视图并手动添加 id,以指定 ID:

Forcing WYSIWYG Ids

Markdown 编辑器

在 Markdown 编辑器中,可以直接插入带 ID 的 HTML,以确保其不会发生变化:

# Old Content
Copyright BookStack Enterprises 2020

# Revised with ID
<p id="bkmrk-include-copyright-text">Copyright BookStack Enterprises 2020</p>

定位包含标签的目标

在某些情况下,你可能会遇到一个包含标签,并希望定位该标签所指向的源页面。 你可以利用 BookStack 的页面直接链接来定位,其格式为 <base_url>/link/<page_id>。 从包含标签中取出数字 ID(位于 @ 之后、# 之前,如果存在的话),然后将其作为上述 URL 格式中的 <page_id> ,即可跳转到源页面。

例如,如果你有包含标签 ,则应访问 URL 路径 /link/5。 使用我们的演示实例,完整的示例 URL 为:https://demo.bookstackapp.com/link/5

包含的技术逻辑

BookStack 会尝试智能处理包含操作,以正确应对不同的内容类型,并在一定程度上符合 HTML 规则。 以下是 BookStack 解析包含内容时所做的部分处理:

  • 如果包含目标是特定章节,BookStack 在大多数情况下会获取并包含该章节的内部内容;但如果该章节需要作为一个整体使用,则会完整使用整个内容块。表格、列表和代码块等均属于此类情况。
  • 如果包含的是完整内容块、完整页面或一段连续内容,BookStack 会尝试调整内容结构,以避免出现嵌套段落,因为嵌套段落在 HTML 中无效,并且可能在浏览器尝试自动修复时引发问题。如果合理,此调整会以位置准确的方式进行,但在某些情况下,内容可能会被移动到其所在章节的前方或后方。
  • 包含内容解析完成后,内容章节的 ID 会进行去重处理,这可能导致页面内容加载后,现有页面内容的 ID 发生动态变化。