@hamster-note/document-parser 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 仓鼠笔记
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@hamster-note/document-parser",
3
+ "version": "0.1.0",
4
+ "description": "",
5
+ "scripts": {
6
+ "build:all": "echo 'no-build'",
7
+ "test": "echo 'no-test'"
8
+ },
9
+ "dependencies": {
10
+ "@hamster-note/types": "^0.1.0"
11
+ },
12
+ "devDependencies": {
13
+ "@eslint/js": "^9.39.2",
14
+ "@system-ui-js/development-base": "^0.1.2",
15
+ "@typescript-eslint/eslint-plugin": "^8.50.0",
16
+ "@typescript-eslint/parser": "^8.50.0",
17
+ "eslint": "^9.39.2",
18
+ "eslint-config-prettier": "^10.1.8",
19
+ "eslint-plugin-jsx-a11y": "^6.10.2",
20
+ "eslint-plugin-prettier": "^5.5.4",
21
+ "eslint-plugin-react": "^7.37.5",
22
+ "eslint-plugin-react-hooks": "^7.0.1",
23
+ "eslint-plugin-sonarjs": "^3.0.5",
24
+ "globals": "^15.14.0",
25
+ "prettier": "^3.7.4",
26
+ "prettier-config-standard": "^7.0.0",
27
+ "typescript": "^5.0.2",
28
+ "typescript-eslint": "^8.50.0"
29
+ },
30
+ "main": "src/index.ts",
31
+ "module": "src/index.ts",
32
+ "types": "src/index.ts",
33
+ "files": [
34
+ "src"
35
+ ],
36
+ "packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
37
+ }
@@ -0,0 +1,13 @@
1
+ import { HamsterPage } from '../Page'
2
+ import { IntermediateOutline } from '@hamster-note/types'
3
+
4
+ export abstract class HamsterDocument {
5
+ // 获取 Page 对象列表
6
+ abstract getPages(): Promise<HamsterPage[]>
7
+ // 获取 Page 对象
8
+ abstract getPage(pageNumber: number): Promise<HamsterPage | undefined>
9
+ // 获取大纲(PDF如果有内置的话)
10
+ abstract getOutline(): Promise<IntermediateOutline | undefined>
11
+ // 获取封面
12
+ abstract getCover(): Promise<HTMLCanvasElement | HTMLImageElement>
13
+ }
@@ -0,0 +1,22 @@
1
+ import { Number2 } from '@hamster-note/types'
2
+
3
+ export enum RenderViews {
4
+ THUMBNAIL = 'thumbnail',
5
+ TEXT = 'text'
6
+ }
7
+
8
+ export interface RenderOptions {
9
+ views: RenderViews[]
10
+ scale: number
11
+ }
12
+
13
+ export abstract class HamsterPage {
14
+ abstract render(
15
+ container: HTMLDivElement,
16
+ options?: RenderOptions
17
+ ): Promise<void>
18
+ abstract getNumber(): number
19
+ abstract getSize(scale: number): Number2
20
+ abstract getPureText(): string
21
+ // abstract getTextDom
22
+ }
@@ -0,0 +1,28 @@
1
+ import { HamsterDocument } from '../Document'
2
+ import { IntermediateDocument } from '@hamster-note/types'
3
+
4
+ export class DocumentParser {
5
+ static readonly ext: string
6
+ // Base static methods to be overridden by concrete parsers
7
+ static async encode(
8
+ _file: File | ArrayBuffer
9
+ ): Promise<IntermediateDocument | HamsterDocument | undefined> {
10
+ return Promise.resolve(undefined)
11
+ }
12
+ static async decode(
13
+ _intermediateDocument: IntermediateDocument
14
+ ): Promise<File | ArrayBuffer | undefined> {
15
+ return Promise.resolve(undefined)
16
+ }
17
+ protected static async toArrayBuffer(
18
+ fileOrBuffer: File | ArrayBuffer
19
+ ): Promise<ArrayBuffer> {
20
+ if (fileOrBuffer instanceof ArrayBuffer) return fileOrBuffer
21
+ return await new Promise<ArrayBuffer>((resolve, reject) => {
22
+ const reader = new FileReader()
23
+ reader.readAsArrayBuffer(fileOrBuffer)
24
+ reader.onload = () => resolve(reader.result as ArrayBuffer)
25
+ reader.onerror = (e) => reject(e)
26
+ })
27
+ }
28
+ }
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from './Document'
2
+ export * from './Page'
3
+ export * from './types/DocumentAnchor'
4
+ export * from './Parser'
5
+ export * from './register'
@@ -0,0 +1,30 @@
1
+ import { DocumentParser } from '../Parser'
2
+
3
+ // Store constructors (classes), not instances
4
+ const parserMap: Map<string, typeof DocumentParser> = new Map()
5
+
6
+ // Register a parser class (constructor) which exposes a static `ext`
7
+ export const registerParser = (parser: typeof DocumentParser) => {
8
+ console.log(parser.ext, parser)
9
+ parserMap.set(parser.ext, parser)
10
+ }
11
+
12
+ export function getParser(file: File): typeof DocumentParser | undefined {
13
+ const extension = file.name.split('.').pop()
14
+ if (!extension) {
15
+ return undefined
16
+ }
17
+ return parserMap.get(extension)
18
+ }
19
+
20
+ export function parse(file: File) {
21
+ const Parser = getParser(file)
22
+ console.log('Parse:', Parser)
23
+ if (!Parser) {
24
+ return undefined
25
+ }
26
+ // call static encode on the parser class
27
+ const res = Parser.encode(file)
28
+ // res.then((_doc) => { /* window.doc1 = _doc */ })
29
+ return res
30
+ }
@@ -0,0 +1,17 @@
1
+ // 文档锚(标记了文字id)
2
+ import { Number2 } from '@hamster-note/types'
3
+
4
+ export interface DocumentAnchorWithTextId {
5
+ pageId: string
6
+ textId?: string
7
+ }
8
+
9
+ // 文档锚(标记了位置)
10
+ export interface DocumentAnchorWithPosition {
11
+ pageId: string
12
+ position: Number2
13
+ }
14
+
15
+ export type DocumentAnchor =
16
+ | DocumentAnchorWithPosition
17
+ | DocumentAnchorWithTextId