hexojs / hexo

A fast, simple & powerful blog framework, powered by Node.js.
https://hexo.io
MIT License
38.83k stars 4.75k forks source link

Support additional options for Backtick Code Block #5299

Closed TimonPeng closed 3 months ago

TimonPeng commented 9 months ago

What does it do?

Support additional options for Backtick Code Block.

Screenshots

Before:

Before

After:

After

Source markdown:

Markdown

Pull request tasks

Issue #4830

Syntax of Backtick Code Block is:

[language] [title] [url] [link text] [additional options]

I was going to simply use split spaces to extract it, but the problem is... If do it with this way, then won't be able to use spaces in the title and link text, which is barely ok, but not what I want.

  1. the first one [language] is sure there's no spaces
  2. the second one [title] might have spaces
  3. the third one [url] must be a link
  4. the fourth one [link text] might have spaces
  5. the fifth one [additional options] must have a colon and probably have spaces

It also would be chaos to matching with regex too, although I did write:

/(\w+)\s+(.+?)\s+(?=http)([\S]+)\s+(.+?)\s+((?:\w+:\S+\s*)+)/

.exec('java title https://google.com google mark:1,4-7,10 wrap:true');

[
  'java title https://google.com google mark:1,4-7,10 wrap:true',
  'java',
  'title',
  'https://google.com',
  'google',
  'mark:1,4-7,10 wrap:true',
  index: 0,
  input: 'java title https://google.com google mark:1,4-7,10 wrap:true',
  groups: undefined
]

.exec('python Backtick Code Syntax https://flask.palletsprojects.com/en/2.3.x/ Flask Docs mark:1,5-7 wrap:true')

[
  'python Backtick Code Syntax https://flask.palletsprojects.com/en/2.3.x/ Flask Docs mark:1,5-7 wrap:true',
  'python',
  'Backtick Code Syntax',
  'https://flask.palletsprojects.com/en/2.3.x/',
  'Flask Docs',
  'mark:1,5-7 wrap:true',
  index: 0,
  input: 'python Backtick Code Syntax https://flask.palletsprojects.com/en/2.3.x/ Flask Docs mark:1,5-7 wrap:true',
  groups: undefined
]

In the end I decided to use the same code logic as the Code Block Plugin(lib/plugins/tag/code.js), which is simpler and easier to synchronise updates and maintain.

github-actions[bot] commented 9 months ago

How to test

git clone -b master https://github.com/TimonPeng/hexo.git
cd hexo
npm install
npm test
D-Sketon commented 9 months ago

can you add some tests?

TimonPeng commented 9 months ago

can you add some tests?

Added, it's looks good to you?

TimonPeng commented 9 months ago

prismjs's mark parameter type is string, so move the array loop to highlightjs file.

https://prismjs.com/plugins/line-highlight/

data-line="1,2-3"
coveralls commented 8 months ago

Coverage Status

coverage: 98.601% (-0.09%) from 98.687% when pulling fee7615171556e422d9d7daed39b36c09e68317e on TimonPeng:master into ad056527cee2c55fa961cc84a56f44ea55404c63 on hexojs:master.