Skip to content

Commit

Permalink
🎨 improve(#23): support new parser option 'formatUrl' to resolve urls…
Browse files Browse the repository at this point in the history
… in the ast (#24)

* ⬆️  chore: upgrade devDependencies

* 🎨  improve: support new parser option 'formatUrl' to resolve urls in the ast

* 🔧  chore: fix tokenizer template
  • Loading branch information
guanghechen authored Jan 26, 2024
1 parent f8ed6c9 commit a2c5ac8
Show file tree
Hide file tree
Showing 22 changed files with 1,915 additions and 1,400 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"overrides": [
{
"files": ["**/*.js"],
"files": ["**/*.js", "**/*.mjs"],
"parser": "@babel/eslint-parser",
"parserOptions": {
"ecmaVersion": 2018,
Expand Down
1 change: 0 additions & 1 deletion jest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,5 @@ export default async function () {
},
},
extensionsToTreatAsEsm: ['.ts', '.mts'],
prettierPath: require.resolve('prettier-2'),
}
}
41 changes: 20 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,34 +48,33 @@
"tokenizers/*"
],
"devDependencies": {
"@babel/core": "^7.22.20",
"@babel/eslint-parser": "^7.22.15",
"@guanghechen/conventional-changelog": "^5.0.1",
"@guanghechen/helper-jest": "^5.0.7",
"@guanghechen/helper-string": "^5.0.7",
"@guanghechen/jest-config": "^5.0.7",
"@guanghechen/rollup-config": "^5.3.0",
"@guanghechen/script-doc-link": "^5.0.9",
"@types/jest": "29.5.5",
"@types/node": "^18.17.17",
"@babel/core": "^7.23.9",
"@babel/eslint-parser": "^7.23.9",
"@guanghechen/conventional-changelog": "^6.0.0-alpha.0",
"@guanghechen/helper-jest": "^6.0.0-alpha.2",
"@guanghechen/helper-string": "^6.0.0-alpha.2",
"@guanghechen/jest-config": "^6.0.0-alpha.2",
"@guanghechen/rollup-config": "^6.0.0-alpha.3",
"@guanghechen/script-doc-link": "^6.0.0-alpha.11",
"@types/jest": "29.5.11",
"@types/node": "^18.19.10",
"cross-env": "^7.0.3",
"eslint": "8.49.0",
"eslint": "8.56.0",
"handlebars": "4.7.8",
"husky": "8.0.3",
"husky": "9.0.6",
"is-ci": "3.0.1",
"jest": "^29.7.0",
"lerna": "7.3.0",
"lint-staged": "14.0.1",
"lerna": "8.0.2",
"lint-staged": "15.2.0",
"npm-run-all": "4.1.5",
"pinst": "3.0.0",
"prettier": "3.0.3",
"prettier-2": "npm:prettier@^2",
"rimraf": "^5.0.1",
"rollup": "^3.29.2",
"ts-jest": "29.1.1",
"ts-node": "10.9.1",
"prettier": "3.2.4",
"rimraf": "^5.0.5",
"rollup": "^4.9.6",
"ts-jest": "29.1.2",
"ts-node": "10.9.2",
"tsconfig-paths": "4.2.0",
"typescript": "5.2.2"
"typescript": "5.3.3"
},
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
Expand Down
6 changes: 4 additions & 2 deletions packages/core-parser/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
IInlineTokenizer,
ITokenizer,
} from '@yozora/core-tokenizer'
import { TokenizerType } from '@yozora/core-tokenizer'
import { TokenizerType, encodeLinkDestination } from '@yozora/core-tokenizer'
import { createProcessor } from './processor'
import type { IParseOptions, IParser } from './types'
import { createPhrasingLineGenerator } from './util/phrasing-line'
Expand Down Expand Up @@ -115,12 +115,13 @@ export class DefaultParser implements IParser {
presetDefinitions: [],
presetFootnoteDefinitions: [],
shouldReservePosition: false,
formatUrl: encodeLinkDestination,
...options,
}
}

public parse(contents: Iterable<string> | string, options: IParseOptions = {}): Root {
const { shouldReservePosition, presetDefinitions, presetFootnoteDefinitions } = {
const { shouldReservePosition, presetDefinitions, presetFootnoteDefinitions, formatUrl } = {
...this.defaultParseOptions,
...options,
}
Expand All @@ -138,6 +139,7 @@ export class DefaultParser implements IParser {
shouldReservePosition,
presetDefinitions,
presetFootnoteDefinitions,
formatUrl,
})
const root: Root = processor.process(linesIterator)
return root
Expand Down
11 changes: 7 additions & 4 deletions packages/core-parser/src/processor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function createProcessor(options: IProcessorOptions): IProcessor {
shouldReservePosition,
presetDefinitions,
presetFootnoteDefinitions,
formatUrl,
} = options

let isIdentifierRegisterAvailable = false
Expand All @@ -51,6 +52,7 @@ export function createProcessor(options: IProcessorOptions): IProcessor {
},
parseBlockApi: {
shouldReservePosition,
formatUrl,
processInlines,
parseBlockTokens,
},
Expand All @@ -64,14 +66,15 @@ export function createProcessor(options: IProcessorOptions): IProcessor {
},
parseInlineApi: {
shouldReservePosition,
getNodePoints: () => _nodePoints,
hasDefinition: identifier => definitionIdentifierSet.has(identifier),
hasFootnoteDefinition: identifier => footnoteIdentifierSet.has(identifier),
parseInlineTokens,
calcPosition: token => ({
start: calcStartPoint(_nodePoints, token.startIndex),
end: calcEndPoint(_nodePoints, token.endIndex - 1),
}),
formatUrl,
getNodePoints: () => _nodePoints,
hasDefinition: identifier => definitionIdentifierSet.has(identifier),
hasFootnoteDefinition: identifier => footnoteIdentifierSet.has(identifier),
parseInlineTokens,
},
})

Expand Down
1 change: 1 addition & 0 deletions packages/core-parser/src/processor/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface IProcessorOptions {
readonly shouldReservePosition: boolean
readonly presetDefinitions: Association[]
readonly presetFootnoteDefinitions: Association[]
readonly formatUrl: (url: string) => string
}

/**
Expand Down
7 changes: 7 additions & 0 deletions packages/core-parser/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ export interface IParseOptions {
* Preset footnote definition meta data list.
*/
readonly presetFootnoteDefinitions?: Association[]

/**
* Format url.
* @param url
* @returns
*/
readonly formatUrl?: (url: string) => string
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/core-tokenizer/src/constant.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-duplicate-enum-values */
export enum TokenizerType {
// Block tokenizer
BLOCK = 'block',
Expand Down
5 changes: 5 additions & 0 deletions packages/core-tokenizer/src/types/parse-block/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export interface IParseBlockPhaseApi {
* Whether it is necessary to reserve the position in the Node produced.
*/
readonly shouldReservePosition: boolean
/**
* Format url.
* @param url
*/
formatUrl(url: string): string
/**
* Process node points into inline nodes.
* @param nodePoints
Expand Down
13 changes: 9 additions & 4 deletions packages/core-tokenizer/src/types/parse-inline/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@ export interface IParseInlinePhaseApi {
* Whether it is necessary to reserve the position in the Node produced.
*/
readonly shouldReservePosition: boolean
/**
* Get the node points.
*/
getNodePoints(): ReadonlyArray<INodePoint>
/**
* Calculate position of token.
* @param interval
*/
calcPosition(interval: Readonly<INodeInterval>): Position
/**
* Format url.
* @param url
*/
formatUrl(url: string): string
/**
* Get the node points.
*/
getNodePoints(): ReadonlyArray<INodePoint>
/**
* Check if there is exists a definition with the given identifier.
* @param identifier
Expand Down
4 changes: 2 additions & 2 deletions packages/invariant/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"devDependencies": {
"cross-env": "^7.0.3",
"jest": "^29.7.0",
"rimraf": "^5.0.1",
"rollup": "^3.29.2"
"rimraf": "^5.0.5",
"rollup": "^4.9.6"
}
}
4 changes: 2 additions & 2 deletions packages/parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"devDependencies": {
"cross-env": "^7.0.3",
"jest": "^29.7.0",
"rimraf": "^5.0.1",
"rollup": "^3.29.2"
"rimraf": "^5.0.5",
"rollup": "^4.9.6"
}
}
10 changes: 5 additions & 5 deletions scaffolds/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
"README.md"
],
"dependencies": {
"@guanghechen/eslint-config": "^5.0.2",
"@guanghechen/eslint-config-ts": "^5.0.7",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-jest": "^27.4.0",
"eslint-plugin-prettier": "^5.0.0"
"@guanghechen/eslint-config": "^6.0.0-alpha.1",
"@guanghechen/eslint-config-ts": "^6.0.0-alpha.5",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-jest": "^27.6.3",
"eslint-plugin-prettier": "^5.1.3"
},
"peerDependencies": {
"eslint": ">=8.0.0",
Expand Down
2 changes: 1 addition & 1 deletion scaffolds/jest-for-tokenizer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@yozora/ast": "^2.3.0",
"@yozora/ast-util": "^2.3.0",
"@yozora/invariant": "^2.3.0",
"globby": "^13.2.2",
"globby": "^14.0.0",
"jest": "^29.7.0"
},
"devDependencies": {
Expand Down
5 changes: 4 additions & 1 deletion scaffolds/template-tokenizer/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import { launch } from '@guanghechen/plop-helper'
import path from 'node:path'
import url from 'node:url'

const __dirname = path.dirname(url.fileURLToPath(import.meta.url))

launch(process.argv, args => ({
configPath: args.plopfile || path.join(__dirname, 'index.js'),
configPath: args.plopfile || path.join(__dirname, 'index.mjs'),
}))
10 changes: 5 additions & 5 deletions scaffolds/template-tokenizer/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ import {
import { toKebabCase, toTrim } from '@guanghechen/helper-string'
import path from 'node:path'
import url from 'node:url'
import manifest from './package.json' assert { type: 'json' }
import manifest from './package.json'

const __dirname = path.dirname(url.fileURLToPath(import.meta.url))

const transformers = {
tokenizerName: toTrim,
}

export default function (plop) {
const preAnswers = resolveNpmPackagePreAnswers({
export default async function (plop) {
const preAnswers = await resolveNpmPackagePreAnswers({
isMonorepo: convertToBoolean(process.env.DEBUG_IS_MONOREPO),
})
const defaultAnswers = { packageVersion: manifest.version }
const { cwd, isMonorepo } = preAnswers
const tokenizerPackageNameRegex = /^(?:[^\\/]+\/)tokenizer-([\w-]+)$/

const prompts = createNpmPackagePrompts(preAnswers, defaultAnswers)
const defaultAnswers = { packageVersion: manifest.version }
const prompts = await createNpmPackagePrompts(preAnswers, defaultAnswers)
prompts.splice(
1,
0,
Expand Down
6 changes: 3 additions & 3 deletions scaffolds/template-tokenizer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
"test:update": "yarn test -u"
},
"dependencies": {
"@guanghechen/helper-option": "^5.0.7",
"@guanghechen/helper-plop": "^5.0.8",
"@guanghechen/helper-string": "^5.0.7"
"@guanghechen/helper-option": "^6.0.0-alpha.1",
"@guanghechen/helper-plop": "^6.0.0-alpha.3",
"@guanghechen/helper-string": "^6.0.0-alpha.2"
},
"devDependencies": {
"cross-env": "^7.0.3",
Expand Down
3 changes: 1 addition & 2 deletions tokenizers/autolink/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { LinkType } from '@yozora/ast'
import type { INodePoint } from '@yozora/character'
import { calcStringFromNodePoints } from '@yozora/character'
import type { IParseInlineHookCreator } from '@yozora/core-tokenizer'
import { encodeLinkDestination } from '@yozora/core-tokenizer'
import type { INode, IThis, IToken, T } from './types'

export const parse: IParseInlineHookCreator<T, IToken, INode, IThis> = function (api) {
Expand All @@ -20,7 +19,7 @@ export const parse: IParseInlineHookCreator<T, IToken, INode, IThis> = function
url = 'mailto:' + url
}

const encodedUrl = encodeLinkDestination(url)
const encodedUrl: string = api.formatUrl(url)
const children: Node[] = api.parseInlineTokens(token.children)
const node: INode = api.shouldReservePosition
? { type: LinkType, position: api.calcPosition(token), url: encodedUrl, children }
Expand Down
3 changes: 1 addition & 2 deletions tokenizers/definition/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { DefinitionType } from '@yozora/ast'
import type { INodePoint } from '@yozora/character'
import { AsciiCodePoint, calcEscapedStringFromNodePoints } from '@yozora/character'
import type { IParseBlockHookCreator } from '@yozora/core-tokenizer'
import { encodeLinkDestination } from '@yozora/core-tokenizer'
import type { INode, IThis, IToken, T } from './types'

export const parse: IParseBlockHookCreator<T, IToken, INode, IThis> = function (api) {
Expand All @@ -26,7 +25,7 @@ export const parse: IParseBlockHookCreator<T, IToken, INode, IThis> = function (
true,
)
: calcEscapedStringFromNodePoints(destinationPoints, 0, destinationPoints.length, true)
const url = encodeLinkDestination(destination)
const url = api.formatUrl(destination)

/**
* Resolve link title
Expand Down
3 changes: 1 addition & 2 deletions tokenizers/image/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ImageType } from '@yozora/ast'
import type { INodePoint } from '@yozora/character'
import { AsciiCodePoint, calcEscapedStringFromNodePoints } from '@yozora/character'
import type { IParseInlineHookCreator } from '@yozora/core-tokenizer'
import { encodeLinkDestination } from '@yozora/core-tokenizer'
import type { INode, IThis, IToken, T } from './types'
import { calcImageAlt } from './util'

Expand All @@ -27,7 +26,7 @@ export const parse: IParseInlineHookCreator<T, IToken, INode, IThis> = function
endIndex,
true,
)
url = encodeLinkDestination(destination)
url = api.formatUrl(destination)
}

// calc alt
Expand Down
3 changes: 1 addition & 2 deletions tokenizers/link/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { LinkType } from '@yozora/ast'
import type { INodePoint } from '@yozora/character'
import { AsciiCodePoint, calcEscapedStringFromNodePoints } from '@yozora/character'
import type { IParseInlineHookCreator } from '@yozora/core-tokenizer'
import { encodeLinkDestination } from '@yozora/core-tokenizer'
import type { INode, IThis, IToken, T } from './types'

export const parse: IParseInlineHookCreator<T, IToken, INode, IThis> = function (api) {
Expand All @@ -26,7 +25,7 @@ export const parse: IParseInlineHookCreator<T, IToken, INode, IThis> = function
endIndex,
true,
)
url = encodeLinkDestination(destination)
url = api.formatUrl(destination)
}

// calc title
Expand Down
Loading

0 comments on commit a2c5ac8

Please sign in to comment.