Skip to content

Commit

Permalink
fix(language-service): transforming volar embedded URIs in markdown (#…
Browse files Browse the repository at this point in the history
…180)

Since we use `volar-embedded-content:` URIs, these need to be properly
processed when they appear in markdown content. For example
`markdown-language-service` does this. They use the syntax
`some-url|width=300`. To support this, the URL is split on the `|`
character before processing and joined afterwards.
  • Loading branch information
remcohaszing committed May 14, 2024
1 parent acda7b6 commit e9471b7
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions packages/language-service/lib/utils/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ export function transformDocumentLinkTarget(target: string, context: ServiceCont
const targetUri = URI.parse(target);
const clearUri = targetUri.with({ fragment: '' }).toString(true);
const decoded = context.decodeEmbeddedDocumentUri(clearUri);
const sourceScript = decoded && context.language.scripts.get(decoded[0]);
const virtualCode = decoded && sourceScript?.generated?.embeddedCodes.get(decoded[1]);
if (!decoded) {
return target;
}

target = decoded[0];
const sourceScript = context.language.scripts.get(target);
const virtualCode = sourceScript?.generated?.embeddedCodes.get(decoded[1]);

if (virtualCode) {
for (const map of context.documents.getMaps(virtualCode)) {
Expand Down Expand Up @@ -55,9 +60,10 @@ export function transformDocumentLinkTarget(target: string, context: ServiceCont
}

export function transformMarkdown(content: string, context: ServiceContext) {
return content.replace(/(\[[^\]]+\])(\([^)]+\))/g, s => {
const match = s.match(/(\[[^\]]+\])(\([^)]+\))/)!;
return `${match[1]}(${transformDocumentLinkTarget(match[2].slice(1, -1), context)})`;
return content.replace(/(?!\()volar-embedded-content:\/\/\w+\/[^)]+/g, (match) => {
const segments = match.split('|');
segments[0] = transformDocumentLinkTarget(segments[0], context);
return segments.join('|');
});
}

Expand Down

0 comments on commit e9471b7

Please sign in to comment.