How can I convert markdown files contains relative link with end with *.md to hugo links?

You can accommodate portable links with a markdown render hook.

This is a detailed example of a render hook for links. It looks first for a page, then for a page resource (example: a PDF file), then a global resource.

mkdir -p layouts/_default/_markup
touch layouts/_default/_markup/render-link.html
layouts/_default/_markup/render-link.html
{{- $attrs := dict }}
{{- $u := urls.Parse .Destination }}

{{- if $u.IsAbs }}
  {{- /* Remote */}}
  {{- $attrs = dict "href" $u.String "rel" "external" }}
{{- else }}
  {{- with $u.Path }}
    {{- with $.Page.GetPage . }}
      {{- /* Page */}}
      {{- $href := .RelPermalink }}
      {{- with $u.RawQuery }}
        {{- $href = printf "%s?%s" $href $u.RawQuery }}
      {{- end }}
      {{- with $u.Fragment }}
        {{- $href = printf "%s#%s" $href $u.Fragment }}
      {{- end }}
      {{- $attrs = dict "href" $href }}
    {{- else }}
      {{- with $.Page.Resources.GetMatch $u.Path }}
        {{- /* Page resource; drop query and fragment */}}
        {{- $attrs = dict "href" .RelPermalink }}
      {{- else }}
        {{- with resources.Get $u.Path }}
          {{- /* Global resource; drop query and fragment */}}
          {{- $attrs = dict "href" .RelPermalink }}
        {{- else }}
          {{- errorf "Unable to resolve reference to %s from %s" $u.Path $.Page.File.Path }}
        {{- end }}
      {{- end }}
    {{- end }}
  {{- else }}
    {{- with $u.Fragment }}
      {{- /* Fragment only; prepend page's relative permalink */}}
      {{- $attrs = dict "href" (printf "%s#%s" $.Page.RelPermalink .) }}
    {{- else }}
      {{- errorf "Unable to resolve reference to %s from %s" $u.Path $.Page.File.Path }}
    {{- end }}
  {{- end }}
{{- end }}

{{- with .Title }}
  {{- $attrs = merge $attrs (dict "title" .) }}
{{- end -}}

<a
{{- range $k, $v := $attrs }}
  {{- printf " %s=%q" $k $v | safeHTMLAttr }}
{{- end -}}
>{{ .Text | safe.HTML }}</a>
{{- /**/ -}}

To use the README.md in the root of your project directory as your home page content, create a symbolic link from content/_index.md to the README.md file.

cd content
ln -s ../README.md _index.md 
cd ..
1 Like