mirror of
				https://github.com/kovetskiy/mark.git
				synced 2025-10-31 19:57:36 +08:00 
			
		
		
		
	Merge pull request #262 from mrueg/fix-relative-links
fix: Support relative links with titleFromH1
This commit is contained in:
		
						commit
						530ff5cc3c
					
				
							
								
								
									
										18
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								main.go
									
									
									
									
									
								
							| @ -181,7 +181,7 @@ func processFile( | ||||
| 
 | ||||
| 	markdown = bytes.ReplaceAll(markdown, []byte("\r\n"), []byte("\n")) | ||||
| 
 | ||||
| 	meta, markdown, err := mark.ExtractMeta(markdown) | ||||
| 	meta, markdown, err := mark.ExtractMeta(markdown, flags.Space, flags.TitleFromH1) | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 	} | ||||
| @ -196,29 +196,17 @@ func processFile( | ||||
| 	} | ||||
| 
 | ||||
| 	if pageID == "" && meta == nil { | ||||
| 		if flags.TitleFromH1 && flags.Space != "" { | ||||
| 			meta = &mark.Meta{} | ||||
| 			meta.Type = "page" | ||||
| 		} else { | ||||
| 		log.Fatal( | ||||
| 			`specified file doesn't contain metadata ` + | ||||
| 				`and URL is not specified via command line ` + | ||||
| 				`or doesn't contain pageId GET-parameter`, | ||||
| 		) | ||||
| 	} | ||||
| 	} | ||||
| 
 | ||||
| 	switch { | ||||
| 	case meta.Space == "" && flags.Space == "": | ||||
| 	if meta.Space == "" { | ||||
| 		log.Fatal( | ||||
| 			"space is not set ('Space' header is not set and '--space' option is not set)", | ||||
| 		) | ||||
| 	case meta.Space == "" && flags.Space != "": | ||||
| 		meta.Space = flags.Space | ||||
| 	} | ||||
| 
 | ||||
| 	if meta.Title == "" && flags.TitleFromH1 { | ||||
| 		meta.Title = mark.ExtractDocumentLeadingH1(markdown) | ||||
| 	} | ||||
| 
 | ||||
| 	if meta.Title == "" { | ||||
| @ -270,7 +258,7 @@ func processFile( | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	links, err := mark.ResolveRelativeLinks(api, meta, markdown, ".") | ||||
| 	links, err := mark.ResolveRelativeLinks(api, meta, markdown, filepath.Dir(file), flags.Space, flags.TitleFromH1) | ||||
| 	if err != nil { | ||||
| 		log.Fatalf(err, "unable to resolve relative links") | ||||
| 	} | ||||
|  | ||||
| @ -29,6 +29,8 @@ func ResolveRelativeLinks( | ||||
| 	meta *Meta, | ||||
| 	markdown []byte, | ||||
| 	base string, | ||||
| 	spaceFromCli string, | ||||
| 	titleFromH1 bool, | ||||
| ) ([]LinkSubstitution, error) { | ||||
| 	matches := parseLinks(string(markdown)) | ||||
| 
 | ||||
| @ -42,7 +44,7 @@ func ResolveRelativeLinks( | ||||
| 			match.hash, | ||||
| 		) | ||||
| 
 | ||||
| 		resolved, err := resolveLink(api, base, match) | ||||
| 		resolved, err := resolveLink(api, base, match, spaceFromCli, titleFromH1) | ||||
| 		if err != nil { | ||||
| 			return nil, karma.Format(err, "resolve link: %q", match.full) | ||||
| 		} | ||||
| @ -64,12 +66,15 @@ func resolveLink( | ||||
| 	api *confluence.API, | ||||
| 	base string, | ||||
| 	link markdownLink, | ||||
| 	spaceFromCli string, | ||||
| 	titleFromH1 bool, | ||||
| ) (string, error) { | ||||
| 	var result string | ||||
| 
 | ||||
| 	if len(link.filename) > 0 { | ||||
| 		filepath := filepath.Join(base, link.filename) | ||||
| 
 | ||||
| 		log.Tracef(nil, "filepath: %s", filepath) | ||||
| 		stat, err := os.Stat(filepath) | ||||
| 		if err != nil { | ||||
| 			return "", nil | ||||
| @ -92,7 +97,7 @@ func resolveLink( | ||||
| 
 | ||||
| 		// This helps to determine if found link points to file that's | ||||
| 		// not markdown or have mark required metadata | ||||
| 		linkMeta, _, err := ExtractMeta(linkContents) | ||||
| 		linkMeta, _, err := ExtractMeta(linkContents, spaceFromCli, titleFromH1) | ||||
| 		if err != nil { | ||||
| 			log.Errorf( | ||||
| 				err, | ||||
| @ -107,6 +112,13 @@ func resolveLink( | ||||
| 			return "", nil | ||||
| 		} | ||||
| 
 | ||||
| 		log.Tracef( | ||||
| 			nil, | ||||
| 			"extracted metadata: space=%s title=%s", | ||||
| 			linkMeta.Space, | ||||
| 			linkMeta.Title, | ||||
| 		) | ||||
| 
 | ||||
| 		result, err = getConfluenceLink(api, linkMeta.Space, linkMeta.Title) | ||||
| 		if err != nil { | ||||
| 			return "", karma.Format( | ||||
|  | ||||
| @ -46,7 +46,7 @@ var ( | ||||
| 	reHeaderPatternMacro = regexp.MustCompile(`<!-- Macro: .*`) | ||||
| ) | ||||
| 
 | ||||
| func ExtractMeta(data []byte) (*Meta, []byte, error) { | ||||
| func ExtractMeta(data []byte, spaceFromCli string, titleFromH1 bool) (*Meta, []byte, error) { | ||||
| 	var ( | ||||
| 		meta   *Meta | ||||
| 		offset int | ||||
| @ -146,6 +146,27 @@ func ExtractMeta(data []byte) (*Meta, []byte, error) { | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	if titleFromH1 || spaceFromCli != "" { | ||||
| 		if meta == nil { | ||||
| 			meta = &Meta{} | ||||
| 		} | ||||
| 
 | ||||
| 		if meta.Type == "" { | ||||
| 			meta.Type = "page" | ||||
| 		} | ||||
| 
 | ||||
| 		if meta.ContentAppearance == "" { | ||||
| 			meta.ContentAppearance = FullWidthContentAppearance // Default to full-width for backwards compatibility | ||||
| 		} | ||||
| 
 | ||||
| 		if titleFromH1 && meta.Title == "" { | ||||
| 			meta.Title = ExtractDocumentLeadingH1(data) | ||||
| 		} | ||||
| 		if spaceFromCli != "" && meta.Space == "" { | ||||
| 			meta.Space = spaceFromCli | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	if meta == nil { | ||||
| 		return nil, data, nil | ||||
| 	} | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Manuel Rüger
						Manuel Rüger