【问题标题】:How do I embed code through my text editor?如何通过文本编辑器嵌入代码?
【发布时间】:2019-07-16 01:10:41
【问题描述】:

我正在运行 Hugo 并使用 Notepad++ 编辑我的页面。我想在这里嵌入一些类似于页面的代码。

我的 Hugo 版本是

Hugo Static Site Generator v0.55.6-A5D4C82D windows/amd64 BuildDate: 2019-05-18T07:57:00Z

我的 config.toml 文件在下面。如您所见,我已将 pygments 选项添加到页面顶部:

pygmentsCodefences = true
pygmentsStyle = "autumn"


baseurl = "https://blakeshurtz.netlify.com/"
title = "Blake Shurtz"
theme = "hugo-creative-portfolio-theme"
languageCode = "en-us"
# Enable comments by entering your Disqus shortname
disqusShortname = ""
# Enable Google Analytics by entering your tracking code
googleAnalytics = ""



[params]
  # Style options: default (pink), blue, green, pink, red, sea, violet
  # Use custom.css for your custom styling
  style = "default"
  description = "Describe your website"
  copyright = "©2019 Blake Shurtz"
  sidebarAbout = [
    "I am a research statistician who enjoys building models and apps.",
    "Originally from the Bay Area, currently based in central CA."
  ]
  # Contact page
     # Since this template is static, the contact form uses www.formspree.io as a
     # proxy. The form makes a POST request to their servers to send the actual
     # email. Visitors can send up to a 1000 emails each month for free.
     #
     # What you need to do for the setup?
     #
     # - set your email address under 'email' below
     # - upload the generated site to your server
     # - send a dummy email yourself to confirm your account
     # - click the confirm link in the email from www.formspree.io
     # - you're done. Happy mailing!
  email = "you@yoursite.com"

    # Optional Matomo analytics (formerly piwik)
      # [params.analytics.matomo]
      #  URL = "https://stats.example.com"
      #  ID = "42"
      #  # Track all subdomains with "*.example.com" (Optional)
      #  domain = "www.example.com"
      #  # Optional integrity check hash
      #  hash = ""

  # Nav links in the side bar
  [[params.navlinks]]
  name = "Home"
  url = "portfolio/"
  home = true

  [[params.navlinks]]
  name = "About"
  url = "about/"

  [[params.navlinks]]
  name = "Get in touch"
  url = "contact/"

  [params.social]
    stackoverflow = "https://stats.stackexchange.com/users/206673/blake-shurtz"
  twitter       = "https://twitter.com/blakeobeans"
  email         = "blakeobeans@gmail.com"
  linkedin      = "https://www.linkedin.com/in/blakeshurtz/"

  github        = "https://github.com/blakeobeans"

谁能给我一个示例,说明我需要在文本编辑器中编写什么才能包含代码?

谢谢!

【问题讨论】:

    标签: hugo


    【解决方案1】:

    我假设您的意思是使用降价语法将文本格式化为代码。

    在代码的开头和结尾用三个反引号括起来。

    ```python (or whatever language)
    code here
    ```
    

    【讨论】:

    • 您实际上不需要指定语言,除非它不会自动检测它。虽然我通常会指定语言
    • 一些网站(如上面的那个,我不以任何方式宣传),具有自动颜色编辑功能。这是怎么做到的?
    【解决方案2】:

    正如 Ambrose Leung 的 answer 所提到的,您可以通过将代码块包装在 3 个反引号中来将代码块包含在 Markdown 中:

    ```language
    some code here
    ```
    

    要获得语法高亮,您可以使用 Hugo 内置的 Chroma。只需将这些行添加到您的 config.toml 文件的顶部(不要让这些名称混淆您,他们说的是 pygments 但适用于 chroma):

    pygmentsCodefences = true
    pygmentsStyle = "pygments"
    

    您可以将pygmentsStyle 值设置为style gallery 中的任何样式。

    【讨论】:

    • 您好 zwbetz,我将这两行添加到 configt.toml 页面的顶部,但无济于事。我不确定为什么它不起作用。
    • 您可能有其他干扰配置选项。编辑您的问题以包含您的 config.toml 文件的内容和您的 hugo 版本,然后我会看看。
    • 嗯。您的配置文件看起来不错。你是如何在 Markdown 文件中编写代码 sn-ps 的?您是否在它们周围加上 3 个反引号并指定语言?
    • 顺便说一句,刚刚克隆了您的theme,将这些配置行添加到exampleSite/config.toml,将降价代码块添加到exampleSite/content/portfolio/work1.md,然后运行cd exampleSite; hugo server --themesDir ../..,并且能够在以下位置看到语法突出显示http://localhost:1313/portfolio/work1/
    • 嗨,我可以通过指定程序语言使其工作。以前我只有反引号,现在我有 ```r
    猜你喜欢
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    相关资源
    最近更新 更多