Skip to content

Commit

Permalink
✨ Add support for implicit paths in id attribute (#1640)
Browse files Browse the repository at this point in the history
  • Loading branch information
misode authored Nov 22, 2024
1 parent 65355b1 commit 162d997
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
1 change: 1 addition & 0 deletions packages/core/src/node/ResourceLocationNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type ResourceLocationOptions =
requireCanonical?: boolean
usageType?: SymbolUsageType
namespacePathSep?: ':' | '.'
implicitPath?: string
}
& ({
category: ResourceLocationCategory
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/processor/binder/builtin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,16 @@ export const dispatchSync = SyncBinder.create<AstNode>((node, ctx) => {

export const resourceLocation = SyncBinder.create<ResourceLocationNode>((node, ctx) => {
const raw = ResourceLocationNode.toString(node, 'full')
const sanitizedRaw = ResourceLocation.lengthen(
let sanitizedRaw = ResourceLocation.lengthen(
node.options.namespacePathSep === '.'
? raw.replace(/\./g, ResourceLocation.NamespacePathSep)
: raw,
)
if (node.options.implicitPath) {
const sepIndex = sanitizedRaw.indexOf(ResourceLocation.NamespacePathSep)
sanitizedRaw = sanitizedRaw.substring(0, sepIndex + 1) + node.options.implicitPath
+ sanitizedRaw.substring(sepIndex + 1)
}
if (node.options.category) {
ctx.symbols.query(
ctx.doc,
Expand Down
27 changes: 13 additions & 14 deletions packages/mcdoc/src/runtime/attribute/builtin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface IdConfig {
tags?: 'allowed' | 'implicit' | 'required'
definition?: boolean
prefix?: '!'
path?: string
empty?: 'allowed'
exclude?: string[]
}
Expand All @@ -19,6 +20,7 @@ const idValidator = validator.alternatives<IdConfig>(
tags: validator.optional(validator.options('allowed', 'implicit', 'required')),
definition: validator.optional(validator.boolean),
prefix: validator.optional(validator.options('!')),
path: validator.optional(validator.string),
empty: validator.optional(validator.options('allowed')),
exclude: validator.optional(validator.alternatives<string[]>(
validator.map(validator.string, v => [v]),
Expand All @@ -36,7 +38,7 @@ const idValidator = validator.alternatives<IdConfig>(
)

function getResourceLocationOptions(
{ registry, tags, definition }: IdConfig,
{ registry, tags, definition, path }: IdConfig,
requireCanonical: boolean,
ctx: core.ContextBase,
typeDef?: core.DeepReadonly<SimplifiedMcdocTypeNoUnion>,
Expand All @@ -60,23 +62,20 @@ function getResourceLocationOptions(
registry = `tag/${registry}`
}
if (tags === 'allowed' || tags === 'required') {
if (core.TaggableResourceLocationCategory.is(registry)) {
return {
category: registry,
requireCanonical,
allowTag: true,
requireTag: tags === 'required',
}
}
} else if (core.ResourceLocationCategory.is(registry)) {
return {
category: registry,
category: registry as core.TaggableResourceLocationCategory,
requireCanonical,
usageType: definition ? 'definition' : 'reference',
allowTag: true,
requireTag: tags === 'required',
implicitPath: path,
}
}
ctx.logger.warn(`[mcdoc id] Unhandled registry ${registry}`)
return undefined
return {
category: registry as core.ResourceLocationCategory,
requireCanonical,
usageType: definition ? 'definition' : 'reference',
implicitPath: path,
}
}

interface IntegerConfig {
Expand Down

0 comments on commit 162d997

Please sign in to comment.