feat: encrypt partial content (#289)

This commit is contained in:
翊小久 2023-11-07 14:50:09 +08:00 committed by GitHub
parent fc3c6f3bb2
commit 1c89e25228
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 179 additions and 0 deletions

View File

@ -24,4 +24,11 @@ context('Frontmatter', {
cy.get('button.collapse')
.should('exist')
})
it('partial content encryption', () => {
cy.visit('/examples/partial-content-encryption')
cy.get('div.decrypt-password-container')
.should('exist')
})
})

View File

@ -0,0 +1,39 @@
---
title: 加密文章部分内容测试 - 密码 valaxy
date: 2023-11-02 10:41:00
categories: Test
---
## 实现文章部分内容加密
使用 [Web Crypto API | MDN](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API)
<!-- valaxy-encrypt-start:valaxy -->
```md
{{ frontmatter }}
```
<!-- valaxy-encrypt-end -->
---
中间未被加密的内容
---
<!-- valaxy-encrypt-start:valaxy -->
::: details 动态渲染的 frontmatter
{{ frontmatter }}
:::
<!-- valaxy-encrypt-end -->
```md
<!-- valaxy-encrypt-start:valaxy -->
This should not be encrypted.
<!-- valaxy-encrypt-end -->
```

View File

@ -0,0 +1,29 @@
---
title: Partial Content Encryption
title_zh-CN: 部分内容加密
toc: true
categories:
- examples
---
```md
<!-- valaxy-encrypt-start:valaxy -->
::: details dynamically rendered frontmatter
{{ frontmatter }}
:::
<!-- valaxy-encrypt-end -->
```
::: zh-CN
渲染结果
:::
::: en
Rendering result
:::
<!-- valaxy-encrypt-start:valaxy -->
::: details dynamically rendered frontmatter
{{ frontmatter }}
:::
<!-- valaxy-encrypt-end -->

View File

@ -435,6 +435,80 @@ codeHeightLimit: 300
示例可参见 [代码块高度限制](/examples/code-height-limit)。
### 内容加密 {lang="zh-CN"}
### Content Encryption {lang="en"}
::: zh-CN
首先在 `site.config.ts` 中开启加密
:::
::: en
Firstly, enable encryption in `site.config.ts`.
:::
```ts {5-7}
import { defineSiteConfig } from 'valaxy'
export default defineSiteConfig({
// ...
encrypt: {
enable: true,
}
})
```
::: zh-CN
- 加密整篇文章
:::
::: en
- encrypt the entire article
:::
::: zh-CN
在文章的 Front Matter 中设置 `password`
:::
::: en
Set `password` in the Front Matter of the article:
:::
```md {2}
---
password: your_password
---
```
::: zh-CN
- 加密部分内容
:::
::: en
- encrypt partial content
:::
::: tip
If you set `password` in Front Matter, partial encryption will be ignored.
:::
::: zh-CN
将待加密的内容包裹在 `<!-- valaxy-encrypt-start:your_password --><!-- valaxy-encrypt-end -->` 中。
:::
::: en
Wrap content to be encrypted in `<!-- valaxy-encrypt-start:your_password --><!-- valaxy-encrypt-end -->`.
:::
::: zh-CN
示例可参见 [部分内容加密](/examples/partial-content-encryption)。
:::
::: en
Examples can be found in [Partial Content Encryption](/examples/partial-content-encryption)。
:::
## 主题配置 {lang="zh-CN"}
## Theme Config {lang="en"}

View File

@ -23,6 +23,9 @@ export default defineValaxyConfig<PressTheme.Config>({
enable: true,
type: 'algolia',
},
encrypt: {
enable: true,
},
},
addons: [

View File

@ -275,6 +275,29 @@ export async function createMarkdownToVueRenderFn(
// handle mainContent, encrypt
const { config: { siteConfig: { encrypt } } } = options
if (encrypt.enable) {
// partial encryption
const encryptRegexp = /<!-- valaxy-encrypt-start:(?<password>\w+) -->(?<content>.*?)<!-- valaxy-encrypt-end -->/gs
const encryptcommentRegexp = /((<!-- valaxy-encrypt-start:\w+ -->)|(<!-- valaxy-encrypt-end -->))/g
if (frontmatter.password) {
mainContentMd = mainContentMd.replaceAll(encryptcommentRegexp, '')
}
else {
const partiallyEncryptedContents: string[] = []
for (const matchArr of mainContentMd.matchAll(encryptRegexp)) {
partiallyEncryptedContents.push(
await encryptContent(matchArr.groups!.content, {
password: matchArr.groups!.password,
iv: encrypt.iv,
salt: encrypt.salt,
}),
)
}
frontmatter.partiallyEncryptedContents = partiallyEncryptedContents.length ? partiallyEncryptedContents : undefined
let i = 0
mainContentMd = mainContentMd.replaceAll(encryptRegexp, () => `<ValaxyDecrypt :encrypted-content="frontmatter.partiallyEncryptedContents[${i++}]" />`)
}
// encrypt the entire article
if (frontmatter.password) {
const encryptedContent = await encryptContent(mainContentMd, {
password: frontmatter.password,

View File

@ -163,6 +163,10 @@ export interface PageFrontMatter extends Record<string, any> {
* @description:zh-CN
*/
encryptedContent?: string
/**
* @description:zh-CN
*/
partiallyEncryptedContents?: string[]
/**
* @description:zh-CN
*/