猿问

在 Go 中的 README.md 中的注释标签之间插入链接

我想在我的README.md文件中的评论标签之间插入链接,因为我正在动态生成链接。我写了一个函数来做到这一点,但问题是它也替换了评论标签。我需要修改我的函数以插入评论标签之间的链接,而不是整体替换它。


//README.md


### HTTP APIs


<!--HTTP-API-start-->


<!--HTTP-API-end-->


### AMQP APIs


<!--AMQP-API-start-->


<!--AMQP-API-end-->

这是我为插入链接而编写的函数。一种可能的解决方案是将注释标签与字符串一起附加httpAPI,AmqpAPI但这不是我想要的,因为它替换了文件中的当前标签。


func GenerateGodocLinkInReadme(amqpLinks string, httpLinks string) {


    path := `../../README.md`

    formattedContent, err := ioutil.ReadFile(path)

    if err != nil {

        panic(err)

    }


    httpAPI := "<!--HTTP-API-start-->" +

        amqpLinks +

        "\n" +

        "<!--HTTP-API-end-->"


    AmqpAPI := "<!--AMQP-API-start-->" +

        httpLinks +

        "\n" +

        "<!--AMQP-API-end-->"


    formattedContent = regexp.MustCompile(`<!--AMQP-API-start-->([\s\S]*?)<!--AMQP-API-end-->`).ReplaceAll(formattedContent, []byte(AmqpAPI))

    exitOnFail(ioutil.WriteFile(path, formattedContent, 0644))

    formattedContent = regexp.MustCompile(`<!--HTTP-API-start-->([\s\S]*?)<!--HTTP-API-end-->`).ReplaceAll(formattedContent, []byte(httpAPI))

    exitOnFail(ioutil.WriteFile(path, formattedContent, 0644))

}

此功能正常工作,但它也替换了评论标签。我需要修改这个函数,以便它在评论标签之间插入链接。


千万里不及你
浏览 315回答 1
1回答

月关宝盒

尝试这个。func GenerateGodocLinkInReadme(amqpLinks string, httpLinks string) {&nbsp; &nbsp; path := `README.md`&nbsp; &nbsp; formattedContent, err := ioutil.ReadFile(path)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; amqpRegex := regexp.MustCompile(`<!--AMQP-API-start-->([\s\S]*?)<!--AMQP-API-end-->`)&nbsp; &nbsp; httpRegex := regexp.MustCompile(`<!--HTTP-API-start-->([\s\S]*?)<!--HTTP-API-end-->`)&nbsp; &nbsp; prevAmqpLinks := string(amqpRegex.FindSubmatch((formattedContent))[1]) // Second index of returns links between tags&nbsp; &nbsp; prevHttpLinks := string(httpRegex.FindSubmatch((formattedContent))[1]) // Second index of returns links between tags&nbsp; &nbsp; httpAPI := prevHttpLinks + httpLinks + "\n"&nbsp; &nbsp; AmqpAPI := prevAmqpLinks + amqpLinks + "\n"&nbsp; &nbsp; formattedContent = amqpRegex.ReplaceAll(formattedContent, []byte(`<!--AMQP-API-start-->` + AmqpAPI + `<!--AMQP-API-end-->`))&nbsp; &nbsp; formattedContent = httpRegex.ReplaceAll(formattedContent, []byte(`<!--HTTP-API-start-->` + httpAPI + `<!--HTTP-API-end-->`))&nbsp; &nbsp; exitOnFail(ioutil.WriteFile(path, formattedContent, 0644))}
随时随地看视频慕课网APP

相关分类

Go
我要回答