Skip to content

Commit de4a4b4

Browse files
chore: bump version to 1.2.0 and add 'useDecimal' option for Abbrlink settings
feat: 支持使用十进制表示 Abbrlink
1 parent 4ae99fa commit de4a4b4

File tree

6 files changed

+44
-13
lines changed

6 files changed

+44
-13
lines changed

manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "abbrlink",
33
"name": "Abbrlink",
4-
"version": "1.1.4",
4+
"version": "1.2.0",
55
"minAppVersion": "1.1.0",
66
"description": "Automatically generate permanent short links for your markdown files.",
77
"author": "Q78KG",

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-plugin-abbrlink",
3-
"version": "1.1.4",
3+
"version": "1.2.0",
44
"description": "An Obsidian plugin to generate permanent links for markdown files",
55
"main": "dist/main.js",
66
"scripts": {

src/main.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const DEFAULT_SETTINGS: AbbrLinkSettings = {
1515
useRandomMode: false,
1616
checkCollision: false,
1717
maxCollisionChecks: 3,
18-
overrideDifferentLength: false
18+
overrideDifferentLength: false,
19+
useDecimal: false
1920
}
2021

2122
export default class AbbrLinkPlugin extends Plugin {
@@ -140,7 +141,7 @@ export default class AbbrLinkPlugin extends Plugin {
140141
}
141142
}
142143

143-
new Notice('Abbrlink 冲突已解决!')
144+
new Notice('已解决 Abbrlink 冲突!')
144145
}
145146

146147
private async processFiles(): Promise<void> {
@@ -250,7 +251,7 @@ class SampleSettingTab extends PluginSettingTab {
250251

251252
new Setting(containerEl)
252253
.setName('跳过已有链接')
253-
.setDesc('如果文件已经包含 Abbrlink,则跳过')
254+
.setDesc('如果文件已经包含 Abbrlink,则跳过为该文件生成 Abbrlink')
254255
.addToggle((toggle) =>
255256
toggle
256257
.setValue(this.plugin.settings.skipExisting)
@@ -276,7 +277,7 @@ class SampleSettingTab extends PluginSettingTab {
276277

277278
new Setting(containerEl)
278279
.setName('随机模式')
279-
.setDesc('使用随机生成的 SHA256 作为 Abbrlink')
280+
.setDesc('使用随机数作为 Abbrlink')
280281
.addToggle((toggle) =>
281282
toggle
282283
.setValue(this.plugin.settings.useRandomMode)
@@ -323,5 +324,17 @@ class SampleSettingTab extends PluginSettingTab {
323324
await this.plugin.saveSettings()
324325
})
325326
)
327+
328+
new Setting(containerEl)
329+
.setName('使用十进制')
330+
.setDesc('使用十进制表示 Abbrlink')
331+
.addToggle((toggle) =>
332+
toggle
333+
.setValue(this.plugin.settings.useDecimal)
334+
.onChange(async (value) => {
335+
this.plugin.settings.useDecimal = value
336+
await this.plugin.saveSettings()
337+
})
338+
)
326339
}
327340
}

src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export interface AbbrLinkSettings {
66
checkCollision: boolean
77
maxCollisionChecks: number
88
overrideDifferentLength: boolean
9+
useDecimal: boolean
910
}

src/utils/hash.ts

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import { TFile } from 'obsidian'
22
import { AbbrLinkSettings } from '../types'
33

4-
export async function generateRandomHash(hashLength: number): Promise<string> {
4+
function hexToDecimal(hex: string, maxLength: number): string {
5+
const decimal = BigInt(`0x${hex}`).toString()
6+
if (decimal.length < maxLength) {
7+
return decimal.padStart(maxLength, '0')
8+
}
9+
return decimal.slice(0, maxLength)
10+
}
11+
12+
export async function generateRandomHash(hashLength: number, useDecimal: boolean = false): Promise<string> {
513
const randomBytes = new Uint8Array(32)
614
window.crypto.getRandomValues(randomBytes)
715

@@ -11,6 +19,9 @@ export async function generateRandomHash(hashLength: number): Promise<string> {
1119
.map((b) => b.toString(16).padStart(2, '0'))
1220
.join('')
1321

22+
if (useDecimal) {
23+
return hexToDecimal(hashHex, hashLength)
24+
}
1425
return hashHex.substring(0, hashLength)
1526
}
1627

@@ -19,7 +30,7 @@ export async function generateSha256(
1930
settings: AbbrLinkSettings
2031
): Promise<string> {
2132
if (settings.useRandomMode) {
22-
return await generateRandomHash(settings.hashLength)
33+
return await generateRandomHash(settings.hashLength, settings.useDecimal)
2334
}
2435

2536
const encoder = new window.TextEncoder()
@@ -31,6 +42,9 @@ export async function generateSha256(
3142
.map((b) => b.toString(16).padStart(2, '0'))
3243
.join('')
3344

45+
if (settings.useDecimal) {
46+
return hexToDecimal(hashHex, settings.hashLength)
47+
}
3448
return hashHex.substring(0, settings.hashLength)
3549
}
3650

@@ -43,10 +57,12 @@ export async function generateUniqueHash(
4357

4458
export async function getExistingAbbrlink(
4559
content: string,
46-
hashLength: number
60+
hashLength: number,
61+
useDecimal: boolean = false
4762
): Promise<string | null> {
48-
const match = content.match(
49-
new RegExp(`abbrlink:\\s*([a-fA-F0-9]{${hashLength}})`)
50-
)
63+
const pattern = useDecimal
64+
? `abbrlink:\\s*(\\d{1,${hashLength}})`
65+
: `abbrlink:\\s*([a-fA-F0-9]{${hashLength}})`
66+
const match = content.match(new RegExp(pattern))
5167
return match ? match[1] : null
5268
}

versions.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
"1.1.1": "0.15.0",
1212
"1.1.2": "0.15.0",
1313
"1.1.3": "0.15.0",
14-
"1.1.4": "1.1.0"
14+
"1.1.4": "1.1.0",
15+
"1.2.0": "1.1.0"
1516
}

0 commit comments

Comments
 (0)