refactor: use ast to parse image node
This commit is contained in:
@@ -18,7 +18,9 @@ module.exports = {
|
|||||||
...pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/' }),
|
...pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/' }),
|
||||||
'^src/(.*)$': '<rootDir>/src/$1',
|
'^src/(.*)$': '<rootDir>/src/$1',
|
||||||
'^test/(.*)$': '<rootDir>/test/$1',
|
'^test/(.*)$': '<rootDir>/test/$1',
|
||||||
src: '<rootDir>/src',
|
'^src$': '<rootDir>/src',
|
||||||
|
'^~/(.*)$': '<rootDir>/src/$1',
|
||||||
|
'^~$': '<rootDir>/src',
|
||||||
'^test$': '<rootDir>/test',
|
'^test$': '<rootDir>/test',
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing'
|
|
||||||
import { DbModule } from '../../processors/database/database.module'
|
|
||||||
import { PostModule } from '../post/post.module'
|
|
||||||
import { CategoryService } from './category.service'
|
|
||||||
|
|
||||||
describe('CategoryService', () => {
|
|
||||||
let service: CategoryService
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
|
||||||
providers: [CategoryService],
|
|
||||||
imports: [PostModule, DbModule],
|
|
||||||
}).compile()
|
|
||||||
|
|
||||||
service = module.get<CategoryService>(CategoryService)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(service).toBeDefined()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing'
|
|
||||||
import { PostController } from './post.controller'
|
|
||||||
|
|
||||||
describe('PostController', () => {
|
|
||||||
let controller: PostController
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
|
||||||
controllers: [PostController],
|
|
||||||
}).compile()
|
|
||||||
|
|
||||||
controller = module.get<PostController>(PostController)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(controller).toBeDefined()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing'
|
|
||||||
import { getFakeCategoryModel } from '~/../test/db-model.mock'
|
|
||||||
import { DbModule } from '../../processors/database/database.module'
|
|
||||||
import { CategoryService } from '../category/category.service'
|
|
||||||
import { PostService } from './post.service'
|
|
||||||
|
|
||||||
describe('PostService', () => {
|
|
||||||
let service: PostService
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
|
||||||
providers: [PostService, CategoryService, getFakeCategoryModel()],
|
|
||||||
imports: [DbModule],
|
|
||||||
}).compile()
|
|
||||||
|
|
||||||
service = module.get<PostService>(PostService)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(service).toBeDefined()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -93,7 +93,7 @@ export class ImageService {
|
|||||||
const size = imageSize(buffer)
|
const size = imageSize(buffer)
|
||||||
|
|
||||||
// get accent color
|
// get accent color
|
||||||
const accent = await getAverageRGB(buffer, size)
|
const accent = await getAverageRGB(buffer)
|
||||||
|
|
||||||
return { size, accent }
|
return { size, accent }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,24 @@
|
|||||||
import Vibrant = require('node-vibrant')
|
import Vibrant = require('node-vibrant')
|
||||||
import { ISizeCalculationResult } from 'image-size/dist/types/interface'
|
import { lexer } from 'marked'
|
||||||
|
|
||||||
//TODO use ast to parse markdown
|
|
||||||
export const pickImagesFromMarkdown = (text: string) => {
|
export const pickImagesFromMarkdown = (text: string) => {
|
||||||
const reg = /(?<=!\[.*\]\()(.+)(?=\))/g
|
const ast = lexer(text)
|
||||||
const images = [] as string[]
|
const images = [] as string[]
|
||||||
for (const r of text.matchAll(reg)) {
|
function pickImage(node: any) {
|
||||||
images.push(r[0])
|
if (node.type === 'image') {
|
||||||
|
images.push(node.href)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (node.tokens && Array.isArray(node.tokens)) {
|
||||||
|
return node.tokens.forEach(pickImage)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
ast.forEach(pickImage)
|
||||||
return images
|
return images
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getAverageRGB(
|
export async function getAverageRGB(
|
||||||
buffer: Buffer,
|
buffer: Buffer,
|
||||||
size: ISizeCalculationResult,
|
|
||||||
): Promise<string | undefined> {
|
): Promise<string | undefined> {
|
||||||
if (!buffer) {
|
if (!buffer) {
|
||||||
return undefined
|
return undefined
|
||||||
|
|||||||
15
test/src/utils/pic.util.spec.ts
Normal file
15
test/src/utils/pic.util.spec.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import { pickImagesFromMarkdown } from '~/utils/pic.util'
|
||||||
|
|
||||||
|
describe('src/utils/pic.util', () => {
|
||||||
|
it('test marked ast', () => {
|
||||||
|
const res = pickImagesFromMarkdown(`
|
||||||
|

|
||||||
|
|
||||||
|

|
||||||
|
`)
|
||||||
|
expect(res).toEqual([
|
||||||
|
'https://cdn.innei.ren/bed/2021/0813211729.jpeg',
|
||||||
|
'https://cdn.innei.ren/bed/2021/0813212633.jpg',
|
||||||
|
])
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -22,7 +22,7 @@
|
|||||||
"~/*": [
|
"~/*": [
|
||||||
"./src/*"
|
"./src/*"
|
||||||
]
|
]
|
||||||
},
|
}
|
||||||
},
|
},
|
||||||
"exclude": [
|
"exclude": [
|
||||||
"dist",
|
"dist",
|
||||||
|
|||||||
Reference in New Issue
Block a user