- 
                Notifications
    You must be signed in to change notification settings 
- Fork 24
Add src-link generator #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            onmax
  wants to merge
  1
  commit into
  unjs:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
onmax:onmax/src-link
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
      
        
          +369
        
        
          −0
        
        
          
        
      
    
  
  
     Open
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # src-link | ||
|  | ||
| The `src-link` generator automatically creates links to specific lines in the GitHub file, or relative to the markdown file, by looking for patterns. | ||
|  | ||
| ## Example | ||
|  | ||
| <!-- automd:example generator=src-link src="gh:nuxt/nuxt/blob/main/packages/schema/src/types/hooks.ts" pattern="export interface NuxtHooks" label="schema source code" --> | ||
|  | ||
| ### Input | ||
|  | ||
| <!-- automd:src-link src="gh:nuxt/nuxt/blob/main/packages/schema/src/types/hooks.ts" pattern="\"export" interface NuxtHooks" label="\"schema" source code" --> | ||
| <!-- /automd --> | ||
|  | ||
| ### Output | ||
|  | ||
| <!-- automd:src-link src="gh:nuxt/nuxt/blob/main/packages/schema/src/types/hooks.ts" pattern="\"export" interface NuxtHooks" label="\"schema" source code" --> | ||
|  | ||
| <!-- ⚠️ Unknown generator:`src-link`. --> | ||
|  | ||
| <!-- /automd --> | ||
|  | ||
| <!-- /automd --> | ||
|  | ||
| ## Arguments | ||
|  | ||
| ::field-group | ||
|  | ||
| ::field{name="src" type="string"} | ||
| The relative path of the file or the GitHub URL where the file is located. If it is a GitHub URL, it should start with `gh:`. | ||
| :: | ||
|  | ||
| ::field{name="pattern" type="string"} | ||
| The pattern to search for in the file. This can be a string or a regular expression. | ||
| :: | ||
|  | ||
| ::field{name="label" type="string"} | ||
| The text for the link to appear in the markdown output. | ||
| :: | ||
|  | ||
| :: | ||
|  | ||
| ## Usage | ||
|  | ||
| Instead of manually maintaining line numbers in your documentation, you can use the `src-link` generator to automatically create links to specific lines in GitHub files or relative to the markdown file. For example: | ||
|  | ||
| ```markdown | ||
| Check the <!-- automd:src-link src="gh:nuxt/nuxt/blob/main/packages/schema/src/types/hooks.ts" pattern="export interface NuxtHooks" label="schema source code" --> for all available hooks. | ||
| ``` | ||
|  | ||
| This will generate a link to the correct line in the file, even if the line number changes in the future. | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import { defineGenerator } from "../generator"; | ||
| import { resolve } from "pathe"; | ||
| import { readFile } from "node:fs/promises"; | ||
|  | ||
| export const srcLink = defineGenerator({ | ||
| name: "src-link", | ||
| async generate({ args, url }) { | ||
| const { pattern, label } = args; | ||
| let src = args.src; | ||
|  | ||
| if (!src || !pattern || !label) { | ||
| throw new Error("src, pattern, and label are required arguments"); | ||
| } | ||
|  | ||
| let originalSrc = src; | ||
| let contents = ""; | ||
|  | ||
| if (src.startsWith("gh:") || src.startsWith("https://github.com/")) { | ||
| const url = new URL( | ||
| src.startsWith("gh:") ? `https://github.com/${src.slice(3)}` : src, | ||
| ); | ||
| originalSrc = url.toString(); | ||
| url.host = "raw.githubusercontent.com"; | ||
| src = url.toString(); | ||
| } else if (src.startsWith("http")) { | ||
| // If it's a URL, we can't fetch the raw content, then we just use the Highlighted URL | ||
| const url = `${src}#:~:text=${encodeURIComponent(pattern)}`; | ||
| return { | ||
| contents: `[${label}](${url})`, | ||
| }; | ||
| } else { | ||
| // Handle local file paths | ||
| try { | ||
| const localFilePath = resolve(url || "", src); | ||
| contents = await readFile(localFilePath, "utf8"); | ||
| } catch (error) { | ||
| return { | ||
| contents: `[${label}](${originalSrc})`, | ||
| issues: [`Failed to read local file: ${originalSrc}. ${error}`], | ||
| }; | ||
| } | ||
| } | ||
|  | ||
| if (!contents) { | ||
| // If contents weren't set by reading a local file, fetch from the network | ||
| try { | ||
| const { $fetch } = await import("ofetch"); | ||
| contents = await $fetch(src); | ||
| } catch (error) { | ||
| return { | ||
| contents: `[${label}](${originalSrc})`, | ||
| issues: [`Failed to fetch file: ${originalSrc}. ${error}`], | ||
| }; | ||
| } | ||
| } | ||
|  | ||
| const re = parsePattern(pattern); | ||
| const matches = [...contents.matchAll(re)]; | ||
| const matchedLines = matches.map( | ||
| (match) => contents.slice(0, match.index).split("\n").length, | ||
| ); | ||
|  | ||
| if (matchedLines.length === 0) { | ||
| return { | ||
| contents: `[${label}](${originalSrc})`, | ||
| issues: [`Pattern "${pattern}" not found in the file: ${originalSrc}`], | ||
| }; | ||
| } | ||
|  | ||
| if (matchedLines.length > 1) { | ||
| return { | ||
| contents: `[${label}](${originalSrc})`, | ||
| issues: [ | ||
| `Multiple matches found for pattern "${pattern}" in the file: ${originalSrc}. Matches found at lines: ${matchedLines.join(", ")}`, | ||
| ], | ||
| }; | ||
| } | ||
|  | ||
| let linkUrl; | ||
| if (originalSrc.startsWith("https://github.com")) { | ||
| const firstMatch = [...matches][0]; | ||
| const matchStartLine = matchedLines[0]; | ||
| const matchEndLine = contents | ||
| .slice(0, firstMatch.index + firstMatch[0].length) | ||
| .split("\n").length; | ||
| const matchHasMultipleLines = matchStartLine !== matchEndLine; | ||
| const lines = matchHasMultipleLines | ||
| ? `L${matchStartLine}-L${matchEndLine}` | ||
| : `L${matchStartLine}`; | ||
| linkUrl = `${originalSrc}#${lines}`; | ||
| } else { | ||
| linkUrl = `${originalSrc}:${matchedLines[0]}`; | ||
| } | ||
|  | ||
| return { | ||
| contents: `[${label}](${linkUrl})`, | ||
| }; | ||
| }, | ||
| }); | ||
|  | ||
| function parsePattern(pattern: string): RegExp { | ||
| let regex; | ||
| let flags = "g"; // Default to global search | ||
|  | ||
| if (pattern.startsWith("/") && pattern.endsWith("/")) { | ||
| // If the pattern is wrapped in slashes, extract it as a regex | ||
| const parts = pattern.split("/"); | ||
| regex = parts[1]; // The actual pattern between the slashes | ||
| flags = parts[2] || "g"; // Any flags provided after the last slash | ||
| } else { | ||
| // Treat as a literal string, escape special regex characters | ||
| regex = pattern.replace(/[$()*+.?[\\\]^{|}]/g, String.raw`\$&`); | ||
| if (pattern.includes("\n")) { | ||
| flags += "m"; // Enable multi-line mode if the pattern contains newlines | ||
| } | ||
| } | ||
| return new RegExp(regex, flags); | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| import { describe, it, expect, vi } from "vitest"; | ||
| import { srcLink } from "../src/generators/src-link"; | ||
|  | ||
|  | ||
| describe("src-link generator", () => { | ||
| vi.mock("ofetch", () => ({ | ||
| $fetch: vi.fn().mockResolvedValue(`{ | ||
| "name": "nuxt-framework", | ||
| "version": "1.0.0", | ||
| "dependencies": {} | ||
| }`), | ||
| })); | ||
|  | ||
| vi.mock('node:fs/promises', () => ({ | ||
| readFile: vi.fn().mockImplementation(() => ''), | ||
| })); | ||
|  | ||
| vi.mock('pathe', () => ({ | ||
| resolve: vi.fn().mockImplementation(() => ''), | ||
| })); | ||
|  | ||
| it("generates correct link for GitHub URL", async () => { | ||
| const result = await srcLink.generate({ | ||
| args: { | ||
| src: "gh:nuxt/framework/blob/main/package.json", | ||
| pattern: "\"name\": \"nuxt-framework\"", | ||
| label: "long live nuxt", | ||
| }, | ||
| config: {} as any, | ||
| block: {} as any, | ||
| transform: async () => ({ contents: "", hasChanged: false, hasIssues: false, updates: [], time: 0 }), | ||
| }); | ||
|  | ||
| expect(result.contents).toBe("[long live nuxt](https://github.com/nuxt/framework/blob/main/package.json#L2)"); | ||
| }); | ||
|  | ||
| it("generates correct link for GitHub URL with a Regex", async () => { | ||
| const re = /name/; | ||
| const result = await srcLink.generate({ | ||
| args: { | ||
| src: "gh:nuxt/framework/blob/main/package.json", | ||
| pattern: re.toString(), | ||
| label: "long live nuxt", | ||
| }, | ||
| config: {} as any, | ||
| block: {} as any, | ||
| transform: async () => ({ contents: "", hasChanged: false, hasIssues: false, updates: [], time: 0 }), | ||
| }); | ||
|  | ||
| expect(result.contents).toBe("[long live nuxt](https://github.com/nuxt/framework/blob/main/package.json#L2)"); | ||
| }); | ||
|  | ||
| it("generates correct link for GitHub URL with multiple lines and Regex", async () => { | ||
| const re = /"name"[^}]*"dependencies"/; | ||
| const result = await srcLink.generate({ | ||
| args: { | ||
| src: "gh:nuxt/framework/blob/main/package.json", | ||
| pattern: re.toString(), | ||
| label: "long live nuxt", | ||
| }, | ||
| config: {} as any, | ||
| block: {} as any, | ||
| transform: async () => ({ contents: "", hasChanged: false, hasIssues: false, updates: [], time: 0 }), | ||
| }); | ||
|  | ||
| expect(result.contents).toBe("[long live nuxt](https://github.com/nuxt/framework/blob/main/package.json#L2-L4)"); | ||
| }); | ||
|  | ||
| it("generates correct link for full GitHub URL", async () => { | ||
| const result = await srcLink.generate({ | ||
| args: { | ||
| src: "https://github.com/nuxt/framework/blob/main/package.json", | ||
| pattern: "\"name\": \"nuxt-framework\"", | ||
| label: "long live nuxt", | ||
| }, | ||
| config: {} as any, | ||
| block: {} as any, | ||
| transform: async () => ({ contents: "", hasChanged: false, hasIssues: false, updates: [], time: 0 }), | ||
| }); | ||
|  | ||
| expect(result.contents).toBe("[long live nuxt](https://github.com/nuxt/framework/blob/main/package.json#L2)"); | ||
| }); | ||
|  | ||
| it("appends link to file but not the line number if no pattern or multiple patterns are found", async () => { | ||
| const resultNonExistent = await srcLink.generate({ | ||
| args: { | ||
| src: "gh:nuxt/framework/blob/main/package.json", | ||
| pattern: "NonexistentPattern", | ||
| label: "long live nuxt", | ||
| }, | ||
| config: {} as any, | ||
| block: {} as any, | ||
| transform: async () => ({ contents: "", hasChanged: false, hasIssues: false, updates: [], time: 0 }), | ||
| }); | ||
|  | ||
| expect(resultNonExistent.contents).toBe("[long live nuxt](https://github.com/nuxt/framework/blob/main/package.json)"); | ||
|  | ||
| const resultMultipleResults = await srcLink.generate({ | ||
| args: { | ||
| src: "gh:nuxt/framework/blob/main/package.json", | ||
| pattern: "\n", | ||
| label: "long live nuxt", | ||
| }, | ||
| config: {} as any, | ||
| block: {} as any, | ||
| transform: async () => ({ contents: "", hasChanged: false, hasIssues: false, updates: [], time: 0 }), | ||
| }); | ||
|  | ||
| expect(resultMultipleResults.contents).toBe("[long live nuxt](https://github.com/nuxt/framework/blob/main/package.json)"); | ||
| }); | ||
|  | ||
| it("throws error when required arguments are missing", async () => { | ||
| await expect(srcLink.generate({ | ||
| args: {}, | ||
| config: {} as any, | ||
| block: {} as any, | ||
| transform: async () => ({ contents: "", hasChanged: false, hasIssues: false, updates: [], time: 0 }), | ||
| })).rejects.toThrow("src, pattern, and label are required arguments"); | ||
| }); | ||
|  | ||
| it("generates correct link using highlight API for non-GitHub URL", async () => { | ||
| const result = await srcLink.generate({ | ||
| args: { | ||
| src: "https://example.com/some-page", | ||
| pattern: "highlighted text", | ||
| label: "Check this out", | ||
| }, | ||
| config: {} as any, | ||
| block: {} as any, | ||
| url: "", | ||
| transform: async () => ({ contents: "", hasChanged: false, hasIssues: false, updates: [], time: 0 }), | ||
| }); | ||
|  | ||
| const expectedUrl = `https://example.com/some-page#:~:text=${encodeURIComponent( | ||
| "highlighted text" | ||
| )}`; | ||
| expect(result.contents).toBe(`[Check this out](${expectedUrl})`); | ||
| }); | ||
|  | ||
| it("generates correct link for local file", async () => { | ||
| const mockReadFile = vi | ||
| .spyOn(await import("node:fs/promises"), "readFile") | ||
| .mockResolvedValue("line1\nline2\nline3\npattern line\nline5"); | ||
|  | ||
| const mockResolve = vi.spyOn(await import("pathe"), "resolve").mockImplementation(() => "/mocked/path/to/local/file"); | ||
|  | ||
| const result = await srcLink.generate({ | ||
| args: { | ||
| src: "/path/to/local/file", | ||
| pattern: "pattern line", | ||
| label: "Local File Label", | ||
| }, | ||
| url: "/current/directory", // Simulate current directory | ||
| config: {} as any, | ||
| block: {} as any, | ||
| transform: async () => ({ | ||
| contents: "", | ||
| hasChanged: false, | ||
| hasIssues: false, | ||
| updates: [], | ||
| time: 0, | ||
| }), | ||
| }); | ||
|  | ||
| expect(mockReadFile).toHaveBeenCalledWith("/mocked/path/to/local/file", "utf8"); | ||
| expect(result.contents).toBe("[Local File Label](/path/to/local/file:4)"); | ||
|  | ||
| mockReadFile.mockRestore(); | ||
| mockResolve.mockRestore(); | ||
| }); | ||
|  | ||
| it("handles local file read error gracefully", async () => { | ||
| const mockReadFile = vi | ||
| .spyOn(await import("node:fs/promises"), "readFile") | ||
| .mockRejectedValue(new Error("File not found")); | ||
|  | ||
| const result = await srcLink.generate({ | ||
| args: { | ||
| src: "/non/existent/file", | ||
| pattern: "pattern", | ||
| label: "Missing File", | ||
| }, | ||
| url: "/current/directory", | ||
| config: {} as any, | ||
| block: {} as any, | ||
| transform: async () => ({ | ||
| contents: "", | ||
| hasChanged: false, | ||
| hasIssues: false, | ||
| updates: [], | ||
| time: 0, | ||
| }), | ||
| }); | ||
|  | ||
| expect(result.contents).toBe("[Missing File](/non/existent/file)"); | ||
|  | ||
| mockReadFile.mockRestore(); | ||
| }); | ||
| }); | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure how to register the generator in the config/automd